iOS collectionview小结

本文总结了iOS中UICollectionView的使用,包括需遵循的协议UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,以及关键函数的实现,如cell数量的计算,自定义头部视图,设置布局属性等,展示了CollectionView在应用中的强大功能。" 110884452,10294605,古诗词知识图谱构建与Neo4j应用,"['知识图谱', '图数据库', 'Neo4j', '数据存储', '数据采集']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

collection view是6之后才有的,用法和tableview很像,但是个人感觉功能更加强大~~

一般使用collectionview要实现三个协议:

UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

第三个是iO给提供的一个封装的类,可以用来实现cell的一些属性功能时使用。

一般情况下要实现的函数:

- (NSInteger) numberOfSectionsInCollectionView : (UICollectionView * )collectionView

设置分组;

- (NSInteger) collectionView : (UICollectionView *)collectionView

      numberOfItemsInSection : (NSInteger)section

设置每组内元素的个数

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

设置页眉页脚的视图,kind可以是header,footer

可以在storyboard中加上一个Collection Reusable View,可以调整位置大小,样式等等,代码和storyboard是通过Identifier联系起来的,比如:

if (kind == UICollectionElementKindSectionHeader) {

        headerView = [_grid dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId" forIndexPath:indexPath];

。。。。

    }

storyboard中页眉的Identifier就是headerId

storyboard- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

设置元素的显示,可以加载一个xib文件,也可以直接在storyboard中的collectionview上加一个collectionviewcell

在view load的时候

UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];

    flowLayout.itemSize = CGSizeMake(80, 80);这其实就是cell的大小

    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;这是设置滑动的方向

    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 0);设置边缘

    flowLayout.headerReferenceSize = CGSizeMake(288, 40);设置页眉,因为我没有用到页脚,所以在这个地方没有定义,在对应的页脚的方法中也不要设置页脚的样式就可以了,如果在这个地方定义了页脚就必须要在- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath*)indexPath函数中设置页脚的样式

    _grid.collectionViewLayout = flowLayout;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值