針對于UICollectionView的使用方法與UITableView類似
UICollectionViewLayout
UICollectionViewLayout決定了UICollectionView如何顯示在界面上,Apple提供了一個(gè)最簡單的默認(rèn)layout對象:UICollectionViewFlowLayout。
Flow Layout是一個(gè)Cells的線性布局方案,并具有頁面和頁腳。其可定制的內(nèi)容如下:
itemSize屬性
設(shè)定全局的Cell尺寸,如果想要單獨(dú)定義某個(gè)Cell的尺寸,可以使用下面方法:
-?(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
minimumLineSpacing屬性
設(shè)定全局的行間距,如果想要設(shè)定指定區(qū)內(nèi)Cell的最小行距,可以使用下面方法:
-?(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
minimumInteritemSpacing屬性
設(shè)定全局的Cell間距,如果想要設(shè)定指定區(qū)內(nèi)Cell的最小間距,可以使用下面方法:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
scrollDirection屬性
設(shè)定滾動(dòng)方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal兩個(gè)值。
headerReferenceSize屬性與footerReferenceSize屬性
設(shè)定頁眉和頁腳的全局尺寸,需要注意的是,根據(jù)滾動(dòng)方向不同,header和footer的width和height中只有一個(gè)會(huì)起作用。如果要單獨(dú)設(shè)置指定區(qū)內(nèi)的頁面和頁腳尺寸,可以使用下面方法:
-?(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
-?(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
sectionInset屬性
設(shè)定全局的區(qū)內(nèi)邊距,如果想要設(shè)定指定區(qū)的內(nèi)邊距,可以使用下面方法:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
然后需要實(shí)現(xiàn)三種類型的委托:UICollectionViewDataSource, UICollectionViewDelagate和UICollectionViewDelegateFlowLayout
因?yàn)閁ICollectionViewDelegateFlowLayout實(shí)際上是UICollectionViewDelegate的一個(gè)子協(xié)議,它繼承了UICollectionViewDelegate,所以只需要在聲明處寫上UICollectionViewDelegateFlowLayout就行了。
UICollectionViewDataSource
-?(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
返回collection view里區(qū)(section)的個(gè)數(shù),如果沒有實(shí)現(xiàn)該方法,將默認(rèn)返回1
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
返回指定區(qū)(section)包含的數(shù)據(jù)源條目數(shù)(number of items),該方法必須實(shí)現(xiàn):
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
返回某個(gè)indexPath對應(yīng)的cell,該方法必須實(shí)現(xiàn):
UICollectionViewCell結(jié)構(gòu)上相對比較簡單,由下至上:
? ? ?a.首先是cell本身作為容器view
? ? ?b. 然后是一個(gè)大小自動(dòng)適應(yīng)整個(gè)cell的backgroundView,用作cell平時(shí)的背景
? ? ?c.再其次是selectedBackgroundView,是cell被選中時(shí)的背景
? ? ?d.最后是一個(gè)contentView,自定義內(nèi)容應(yīng)被加在這個(gè)view上
為collection view添加一個(gè)補(bǔ)充視圖(頁眉或頁腳)
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
設(shè)定頁眉的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section:
設(shè)定頁腳的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section:
添加頁眉和頁腳以前需要注冊類和標(biāo)識(shí):
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString*)elementKind withReuseIdentifier:(NSString*)identifier:
UICollectionViewDelegateFlowLayout
設(shè)定指定Cell的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath:
設(shè)定collectionView(指定區(qū))的邊距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
設(shè)定指定區(qū)內(nèi)Cell的最小行距,也可以直接設(shè)置UICollectionViewFlowLayout的minimumLineSpacing屬性
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
設(shè)定指定區(qū)內(nèi)Cell的最小間距,也可以直接設(shè)置UICollectionViewFlowLayout的minimumInteritemSpacing屬性
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
UICollectionViewDelegate
當(dāng)指定indexPath處的item被選擇時(shí)觸發(fā)
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
注意:當(dāng)你刪除或添加元素時(shí),一定要更新numberOfItemsInSection的返回情況。
當(dāng)指定indexPath處的item被取消選擇時(shí)觸發(fā),僅在允許多選時(shí)被調(diào)用
- (void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath*)indexPath
下面是三個(gè)和高亮有關(guān)的方法:
-?(BOOL)collectionView:(UICollectionView*)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath
-?(void)collectionView:(UICollectionView*)collectionView didHighlightItemAtIndexPath:(NSIndexPath*)indexPath
-?(void)collectionView:(UICollectionView*)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath*)indexPath
事件的處理順序如下:
? ?a.手指按下
? ?b.shouldHighlightItemAtIndexPath (如果返回YES則向下執(zhí)行,否則執(zhí)行到這里為止)
? ?c.didHighlightItemAtIndexPath (高亮)?
? ?d.手指松開
? ?e.didUnhighlightItemAtIndexPath (取消高亮)
? ?f.shouldSelectItemAtIndexPath (如果返回YES則向下執(zhí)行,否則執(zhí)行到這里為止)
? ?g.didSelectItemAtIndexPath (執(zhí)行選擇事件)