collectionView的每一组的组头部和尾部的设置

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{

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

    _flowLayout = flowLayout;

    _flowLayout.minimumLineSpacing = 3;

    _flowLayout.minimumInteritemSpacing = 3;

    _flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 35);

    _flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 10);

 

    self = [super initWithFrame:CGRectZero collectionViewLayout:_flowLayout];

   

    

    if (self) {

        

        self.showsVerticalScrollIndicator = NO;

        self.bounces = NO;

        self.alwaysBounceVertical = NO;

        self.scrollsToTop = YES;

       // self.pagingEnabled = NO;

        

    

        self.dataSource = self;

        self.delegate = self;

        

        //self.backgroundColor = [UIColor clearColor];

        self.backgroundColor = OtherLineColor;

        //注册cell单元格 

        [self registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

   //下面的注册组的头尾部view,这个必须实现,否则会崩   

        //注册header单元格

        [self registerClass:[CategoryCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ID];

        //注册footer单元格

        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footID];

 

    }

    return self;

}

 

//组的头尾部的代码实现

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

    UICollectionReusableView *reusableView = nil;

//组的头部

    if (kind == UICollectionElementKindSectionHeader ) {

//CategoryCollectionReusableView是自定义的继承UICollectionReusableView的子类;

    CategoryCollectionReusableView *categoryCollectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ID forIndexPath:indexPath];

    if (indexPath.section == 0) {

        categoryCollectionReusableView.title = @"全部分类";

        

    }else if (indexPath.section == 1){

        categoryCollectionReusableView.title = @"热门品牌";

    }

        reusableView = categoryCollectionReusableView;

    }

    //组的尾部

    if (kind == UICollectionElementKindSectionFooter) {

        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footID forIndexPath:indexPath];

        reusableView = footer;

    }

    

    return reusableView;

}

转载于:https://my.oschina.net/llfk/blog/827227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值