UICollectionView为不同section设置不同header

用继承自UICollectionReusableView的xib文件创建headerView,然后用registerNib forSupplementaryViewOfKind方法注册header,最后在 collectionView viewForSupplementaryElementOfKind方法中返回reusableview即可
见如下代码

//注册header
//当headerView为代码创建时
[self.collectionView registerClass:[XXCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier: headerID];
//当headerView为xib创建时
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XXCell class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
//section=0时的headerView
        if (indexPath.section == 0) {
            XXReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
            reusableview.materialsDetailModel = self.materialsDetailModel;
            return reusableview;
///section=1时的headerView
        } else if (indexPath.section == 1) {
            XXReusableView2 *reusableview2 = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID2 forIndexPath:indexPath];
            return reusableview2;
        }
//footerView
    } else if (kind == UICollectionElementKindSectionFooter){
        if (indexPath.section == 0) {
            return 0;
        } else {
            XXFooterReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID forIndexPath:indexPath];
            footerView.remark = self.materialsDetailModel.remark;
            return footerView;
        }
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容