UICollectionView中的dataSource、delegate、delegateFlowLayout常用方法說明

1、UICollectionViewDataSource

01、- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

必須要實現的方法,返回的值是在每一個分組(分區)中包含的單元格的個數。

02、- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

必須要實現的方法,返回的值是在每一個分組(分區)中包含的在每一個位置的單元格的內容,并且必須是使用dequeuereusablecellwithreuseidentifier方法中找出重用單元格。

03、- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

不是必須要實現的方法,返回的值是一個collectionView中的分組(分區)個數。我們自己不實現的話,系統默認返回值為1。

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

不是必須要實現的方法,返回的值是collectionView中的分組(分區)的頭部試圖view或者尾部視圖view。我們自己不實現的話,系統默認返回值為nil。

05、- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);

不是必須要實現的方法,詢問是否指定的單元格項目是否可以移動到集合視圖中的另一個位置,默認返回值為NO。

06、- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);

不是必須要實現的方法,將指定的單元格項目從一個位置移動到集合視圖中的另一個位置。

2、UICollectionViewDelegate

UICollectionViewDelegate中的協議方法全都不是必須要實現的,我們自己選擇是否實現。

01、下面是三個和高亮有關的方法:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath

事件的處理順序如下:

01、手指按下:shouldHighlightItemAtIndexPath (如果返回YES則向下執行,否則執行到這里為止)

02、didHighlightItemAtIndexPath (高亮)

03、手指松開:didUnhighlightItemAtIndexPath (取消高亮)

04、shouldSelectItemAtIndexPath (如果返回YES則向下執行,否則執行到這里為止)

05、didSelectItemAtIndexPath (執行選擇事件)

如果只是簡單實現點擊后cell改變顯示狀態,只需要在cellForItemAtIndexPath方法里返回cell時,指定cell的selectedBackgroundView;如果要實現點擊時(手指未松開)的顯示狀態與點擊后(手指松開)的顯示狀態,則需要通過上面提到的方法來實現。

02、- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

當指定indexPath處的item被選擇時觸發,調用該方法

03、- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

當指定indexPath處的item被取消選擇時觸發,僅在允許多選時被調用

04、- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);

這兩個方法分別是指定indexPath的cell將要顯示出的時候調用和指定indexPath的頭部或尾部視圖view將要顯示出來的時候調用

05、- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

這兩個方法分別是指定indexPath的cell將要從collectionView中移除的的時候調用和指定indexPath的頭部或尾部視圖view將要collectionView中移除的時候調用

3、UICollectionViewDelegateFlowLayout

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

設置指定indexPath的單元格的大小

02、- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

設置分組中的每一個單元格的上下左右的空白距離,

03、- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

設置分組中的單元格的行間距

04、- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

設置每行中的cell的間距

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

分組的頭部視圖的size大小

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

分組的尾部視圖的size大小

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容