UICollectionView添加頭視圖

UICollectionView與UITableView一樣都需要遵從代理和數(shù)據(jù)源的方法。

1.UICollectionView 創(chuàng)建之前需要先寫布局,每個collection都需要遵從這個布局。

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

CGFloat itemW = kScreenWidth/3;

CGFloat itemH = 0.85*itemW + 20;

layout.itemSize = CGSizeMake(itemW, itemH);

layout.headerReferenceSize = CGSizeMake(0, 10); //頭視圖的大小參數(shù)

layout.minimumInteritemSpacing = 0;

layout.minimumLineSpacing = 0; // cell 間距


2.然后注冊頭視圖

[_collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];

3.代理方法

//創(chuàng)建collection頭視圖

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

{

UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

header.backgroundColor = ZJColorFromRGB(0xf6f6f6);

return header;

}

// 設(shè)置section頭視圖的參考大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

return CGSizeMake(kScreenWidth, 10);

}

如果需要自定義頭視圖實現(xiàn)具體功能,可以繼承UICollectionReusableView來實現(xiàn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容