storyboard UICollectionView

本文介绍如何在iOS开发中,利用Storyboard实现UICollectionView的自定义Cell及Section Header,避免额外的XIB文件创建,通过简单步骤完成单元格及头部视图的定制。

场景:在开发中,在storyboard中创建一个UIViewController,又在上面拖进去了UICollectionView控件,这时想要使用自定义的cell和sectionHeader 并且不想单独创建子类的.xib文件

 

在storyboard外创建单元格和section头视图的子类(.h和.m),在stroyboard对应的cell和section的归属设为上一步创建的,并设置复用的identifier

 

使用的时候,不用再次注册单元格和复用的头尾视图,只用在对应的代理方法直接寻找就好了

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

    

    NSString *sectionidentifier = NSStringFromClass([CollectionSectionCell class]);

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        

        CollectionSectionCell *section = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionidentifier forIndexPath:indexPath];

        return section;    //返回头视图

    }

    

    return nil;

}

 

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

    NSString *cellidentifier = NSStringFromClass([FavoriteCell class]);

    FavoriteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellidentifier forIndexPath:indexPath];

    return cell;

}

转载于:https://www.cnblogs.com/binbins/p/6274468.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值