iOS開發(fā)中經(jīng)常用到UICollectionView,許多人對其需要實現(xiàn)的協(xié)議方法并不熟悉,今天我們一起來學(xué)習(xí)一下當中的一些經(jīng)常用到的方法。
1、UICollectionViewDataSource
01、- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
必須要實現(xiàn)的方法,返回的值是在每一個分組(分區(qū))中包含的單元格的個數(shù)。
02、- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
必須要實現(xiàn)的方法,返回的值是在每一個分組(分區(qū))中包含的在每一個位置的單元格的內(nèi)容,并且必須是使用dequeuereusablecellwithreuseidentifier方法中找出重用單元格。
03、- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
不是必須要實現(xiàn)的方法,返回的值是一個collectionView中的分組(分區(qū))個數(shù)。我們自己不實現(xiàn)的話,系統(tǒng)默認返回值為1。
04、- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
不是必須要實現(xiàn)的方法,返回的值是collectionView中的分組(分區(qū))的頭部試圖view或者尾部視圖view。我們自己不實現(xiàn)的話,系統(tǒng)默認返回值為nil。
05、- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
不是必須要實現(xiàn)的方法,詢問是否指定的單元格項目是否可以移動到集合視圖中的另一個位置,默認返回值為NO。
06、- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
不是必須要實現(xiàn)的方法,將指定的單元格項目從一個位置移動到集合視圖中的另一個位置。
2、UICollectionViewDelegate
UICollectionViewDelegate中的協(xié)議方法全都不是必須要實現(xiàn)的,我們自己選擇是否實現(xiàn)。
01、下面是三個和高亮有關(guān)的方法:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
事件的處理順序如下:
01、手指按下:shouldHighlightItemAtIndexPath (如果返回YES則向下執(zhí)行,否則執(zhí)行到這里為止)
02、didHighlightItemAtIndexPath (高亮)
03、手指松開:didUnhighlightItemAtIndexPath (取消高亮)
04、shouldSelectItemAtIndexPath (如果返回YES則向下執(zhí)行,否則執(zhí)行到這里為止)
05、didSelectItemAtIndexPath (執(zhí)行選擇事件)
如果只是簡單實現(xiàn)點擊后cell改變顯示狀態(tài),只需要在cellForItemAtIndexPath方法里返回cell時,指定cell的selectedBackgroundView;如果要實現(xiàn)點擊時(手指未松開)的顯示狀態(tài)與點擊后(手指松開)的顯示狀態(tài),則需要通過上面提到的方法來實現(xiàn)。
02、- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
當指定indexPath處的item被選擇時觸發(fā),調(diào)用該方法
03、- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
當指定indexPath處的item被取消選擇時觸發(fā),僅在允許多選時被調(diào)用
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將要顯示出的時候調(diào)用和指定indexPath的頭部或尾部視圖view將要顯示出來的時候調(diào)用
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中移除的的時候調(diào)用和指定indexPath的頭部或尾部視圖view將要collectionView中移除的時候調(diào)用
3、UICollectionViewDelegateFlowLayout
01- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
設(shè)置指定indexPath的單元格的大小
02、- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
設(shè)置分組中的每一個單元格的上下左右的空白距離,
03、- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
設(shè)置分組中的單元格的行間距
04、- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
設(shè)置每行中的cell的間距
05、- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
分組的頭部視圖的size大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
分組的尾部視圖的size大小