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;
}

你可能感兴趣的:(UICollectionView为不同section设置不同header)