iOS 10 UICollectionView的简单使用

最近项目中使用到UICollectionView,觉得UICollectionView在项目中的使用频率还是很高,能够省去一些麻烦的写法,在此对UICollectionView的一些方法做一个记录,便于之后开发中的使用。其中有些UICollectionView的API是没有代码提示的,必须从别处直接粘贴进代码才生效,不知道是隐藏API的原因还是什么,有知道的小伙伴可以告诉我下。

1.这三个方法跟UITableView的类似,设置分区、每个分区有几个item、设置item

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 20;
}

这里使用了自定义的UICollectionViewCell,自定义UICollectionViewCell需要重写它的initWithFrame方法,如果是重写init方法,cell是无法创建。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    RentPianoCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:kRentPianoCollectionViewCell forIndexPath:indexPath];
    
    return cell;
}

重写之后还有一点很重要,就是要注册cell,注册代码如下:

    [self.collectionView registerClass:[RentPianoCollectionViewCell class] forCellWithReuseIdentifier:kRentPianoCollectionViewCell];

2.设置item的大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(147, 189);
}

3.设置分区的headerView / footerView,类似UITableView的sectionHeaderView / sectionFooterView。UICollectionView的分区view需要创建继承于UICollectionReusableView的对象,重写其initWithFram方法完成对分区view的内容编辑,编辑完成之后在控制器页面调用以下方法:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        RentPianoReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        
        reusableView = header;
    }
    
    if (kind == UICollectionElementKindSectionFooter)
    {
        RentPianoReusableFooterView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        reusableView = footerview;
    }
    
    return reusableView;
}

UICollectionReusableView同UICollectionViewCell一样,需要注册,使用UICollectionElementKindSectionFooter / UICollectionElementKindSectionHeader来告知其是headerView还是footerView:

 [self.collectionView registerClass:[RentPianoReusableFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];

  [self.collectionView registerClass:[RentPianoReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}

4.设置section头/尾视图的参考大小,这两个API是没有代码提示的,不清楚是什么原因

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(K_ScreenWidth, 208);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section {
    
    return CGSizeMake(K_ScreenWidth, 48);
}

5.设置一个section的上左下右边距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section  
{  
    return UIEdgeInsetsMake(-64, 0, 0, 0);  
} 

题外话:因项目需要,需要在A控制器坐标的(0,0)点放置一个UITableView,但是当切换到B控制器后再切回到A控制器,发现A控制器的tableview的contentOffset自动调整了20,执行以下代码即可解决:

self.edgesForExtendedLayout = UIRectEdgeNone;

edgesForExtendedLayout的具体作用可以自行百度,不做详细介绍

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容