初始化:
//初始化布局類(UICollectionViewLayout的子類)
UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
//初始化collectionView
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
//設(shè)置代理
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
需要實(shí)現(xiàn)的協(xié)議:
UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
UICollectionViewDelegateFlowLayout是UICollectionViewDelegate的子協(xié)議
注冊(cè)相應(yīng)的UICollectionViewCell子類到collectionView用來從隊(duì)列提取和顯示
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
如果是用nib創(chuàng)建的話,使用下面這個(gè)函數(shù)來注冊(cè)。
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
如果需要顯示每個(gè)section的headerView或footerView,則還需注冊(cè)相應(yīng)的UICollectionReusableView的子類到collectionView
elementKind是header或footer的標(biāo)識(shí)符,只有兩種可以設(shè)置UICollectionElementKindSectionHeader和UICollectionElementKindSectionFooter
-(void)registerClass:(Class)viewClass
forSupplementaryViewOfKind:(NSString *)elementKind
withReuseIdentifier:(NSString *)identifier;
如果是用nib創(chuàng)建的話,使用下面這個(gè)函數(shù)來注冊(cè)。
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
實(shí)現(xiàn)協(xié)議的函數(shù):
跟UITableView的DataSource和Delegate很像,大可自行代入理解。
DataSource:
//每一組有多少個(gè)cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//定義并返回每個(gè)cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//collectionView里有多少個(gè)組
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//定義并返回每個(gè)headerView或footerView
-(UICollectionReusableView *)collectionView:(UICollectionView
*)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath;
上面這個(gè)方法使用時(shí)必須要注意的一點(diǎn)是,如果布局沒有為headerView或footerView設(shè)置size的話(默認(rèn)size為CGSizeZero),則該方法不會(huì)被調(diào)用。所以如果需要顯示header或footer,需要手動(dòng)設(shè)置size。
可以通過設(shè)置UICollectionViewFlowLayout的headerReferenceSize和footerReferenceSize屬性來全局控制size。或者通過重載以下代理方法來分別設(shè)置
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section;
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section;
Delegate:
//每一個(gè)cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//設(shè)置每組的cell的邊界, 具體看下圖
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;


//cell的最小行間距
-(CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section;
//cell的最小列間距
-(CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//cell被選擇時(shí)被調(diào)用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//cell反選時(shí)被調(diào)用(多選時(shí)才生效)
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;