UICollectionView為不同section設置不同header

用繼承自UICollectionReusableView的xib文件創(chuàng)建headerView,然后用registerNib forSupplementaryViewOfKind方法注冊header,最后在 collectionView viewForSupplementaryElementOfKind方法中返回reusableview即可
見如下代碼

//注冊header
//當headerView為代碼創(chuàng)建時
[self.collectionView registerClass:[XXCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier: headerID];
//當headerView為xib創(chuàng)建時
[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;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容