一:創(chuàng)建:
? ? ? 必須要有一個(gè)UICollectionViewLayout來(lái)初始化,通常使用UICollectionViewFlowLayout流布局就可以了,有需要就自定義UICollectionViewLayout(比較復(fù)雜)。
二:關(guān)于cell:
1.全局的重用標(biāo)識(shí)符identifier:cell需要,UICollectionReusableView也需要。
2.注冊(cè)cell:
? ? a. 注冊(cè)代碼自定義的cell:
// 注冊(cè)cell
?[_collectionView_ registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifier];
// 注冊(cè)UICollectionReusableView
[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
[_collectionView_ registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
? ? ?b.注冊(cè)xib寫(xiě)的cell:
[_collectionView_ registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:identifier];
3.生成cell:
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
只需要這個(gè)方法,就可以生成cell,且不需要判斷cell是否存在,如果cell不存在,會(huì)自己去生成,然后操作cell的內(nèi)容就好了。加載xib的cell也是這個(gè)方法,這就是與UITableView的區(qū)別。
4.cell的大小:遵循UICollectionViewDelegateFlowLayout協(xié)議
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
cell之間的間距什么的也都在這個(gè)協(xié)議的方法內(nèi)設(shè)置。
其余的代理方法與UITableView類(lèi)似。
三:其他:
a. 設(shè)置collectionView橫向滑動(dòng):
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
b.設(shè)置collectionView不可彈回
collectionView.bounces = NO;