UICollectionView的基本使用

一:創(chuàng)建:

? ? ? 必須要有一個UICollectionViewLayout來初始化,通常使用UICollectionViewFlowLayout流布局就可以了,有需要就自定義UICollectionViewLayout(比較復(fù)雜)。

二:關(guān)于cell:

1.全局的重用標識符identifier:cell需要,UICollectionReusableView也需要。

2.注冊cell:

? ? a. 注冊代碼自定義的cell:

// 注冊cell

?[_collectionView_ registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifier];

// 注冊UICollectionReusableView

[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];

[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];


? ? ?b.注冊xib寫的cell:

[_collectionView_ registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];

3.生成cell:

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

只需要這個方法,就可以生成cell,且不需要判斷cell是否存在,如果cell不存在,會自己去生成,然后操作cell的內(nèi)容就好了。加載xib的cell也是這個方法,這就是與UITableView的區(qū)別。

4.cell的大?。鹤裱璘ICollectionViewDelegateFlowLayout協(xié)議

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

cell之間的間距什么的也都在這個協(xié)議的方法內(nèi)設(shè)置。

其余的代理方法與UITableView類似。

三:其他:

a. 設(shè)置collectionView橫向滑動:

layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

b.設(shè)置collectionView不可彈回

collectionView.bounces = NO;

最后編輯于
?著作權(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)容