iOS collectionview小结

来源:互联网 发布:node服务器 编辑:程序博客网 时间:2024/05/22 01:59

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 = [_griddequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerId"forIndexPath:indexPath];

。。。。

    }

storyboard中页眉的Identifier就是headerId

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

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

在view load的时候

UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayoutalloc]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;




0 0
原创粉丝点击