天天看點

UICollectionView詳解之基礎使用介紹UICollectionView簡介

UICollectionView簡介

UICollectionView在iOS6.0以後引進,與UITableView比較相似。但是UICollectionView将布局完全交給UICollectionViewLayout,而且允許使用者自定義layout來進行布局。使其功能比UITableView更加強大、布局更加靈活,迅速在各大APP廣泛使用。

本篇将對UICollectionView的使用進行簡單介紹,主要包括UICollectionView的視圖組成、資料源、常用代理方法以及UICollectionViewFlowLayout的使用。下一遍将會詳細講解UICollectionViewLayout的自定義布局。

UICollectionView的視圖組成:

UICollectionView由cell視圖、Supplementary View和Decoration View三部分組成。

cell視圖

這個大家應該都很清楚,UICollectionView主要内容展示的視圖

Supplementary View

對于每組section的資訊視圖,類似于UITableView的header或者footer

Decoration View

裝飾視圖,好像很少使用

cell視圖跟Supplementary View使用是需要先進行注冊以便實作複用:

注冊cell的方法,第一個是代碼建立,第二個是xib建立
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;

注冊Supplementary View的方法,第一個是代碼建立,第二個是xib建立
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

在資料源回調中複用的方法:
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

***上面方法的的kind和elementKind參數用來區分是頭部視圖還是尾部視圖
           

UICollectionView的資料源代理方法:

//必須實作的資料源代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;


//可選資料源代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
處理移動相關(詳細使用看demo)
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(_0);
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(_0);
           

UICollectionView的常用代理方法:

//點選cell高亮相關
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;


//cell選中相關
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;


//cell以及Supplementary View繪制相關
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(_0);
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(_0);
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
***這幾個方法可以添加簡易的動畫,例如添加如下幾句代碼,cell加載的時候将會有放縮等效果。
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(_0)
{
    cell.contentView.alpha = ;
    cell.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(, ), );

    [UIView animateKeyframesWithDuration: delay: options: animations:^{

        /**
         *  分步動畫   第一個參數是該動畫開始的百分比時間  第二個參數是該動畫持續的百分比時間
         */
        [UIView addKeyframeWithRelativeStartTime: relativeDuration: animations:^{
            cell.contentView.alpha = ;
            cell.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(, ), );
        }];
        [UIView addKeyframeWithRelativeStartTime: relativeDuration: animations:^{
            cell.contentView.alpha = ;
            cell.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(, ), );

        }];

    } completion:^(BOOL finished) {

    }];
}

//長按顯示菜單相關:(demo中有詳細使用)
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender;
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender;

//轉場相關(下一篇會講解)
- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout;
           

UICollectionViewFlowLayout && UICollectionViewLayout:

UICollectionView之是以比UITableView強大,主要在于其精髓UICollectionViewLayout的布局。它通過 UICollectionViewLayoutAttributes 類來管理 cell 、 Supplementary View 和 Decoration View 的 位置 、 transform 、 alpha 、 hidden 等等。先看看系統為我們提供的UICollectionViewFlowLayout布局的使用吧。

UICollectionViewFlowLayout 的常用屬性

//決定了每行的間隔
@property (nonatomic) CGFloat minimumLineSpacing;
//決定了Item之間的間隔
@property (nonatomic) CGFloat minimumInteritemSpacing;
//cell的大小(如果實作的代理該設定無效)
@property (nonatomic) CGSize itemSize;
//cell大小的預計算
@property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes:
//布局的方向
@property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
//頭視圖跟尾視圖的大小(橫向的時候,headerReferenceSize.width有效,headerReferenceSize.height無效。縱向時相反)
@property (nonatomic) CGSize headerReferenceSize;
@property (nonatomic) CGSize footerReferenceSize;
//間距
@property (nonatomic) UIEdgeInsets sectionInset;

// Set these properties to YES to get headers that pin to the top of the screen and footers that pin to the bottom while scrolling (similar to UITableView).
@property (nonatomic) BOOL sectionHeadersPinToVisibleBounds NS_AVAILABLE_IOS(_0);
@property (nonatomic) BOOL sectionFootersPinToVisibleBounds NS_AVAILABLE_IOS(_0);
           

UICollectionViewFlowLayout 的代理方法

//基本就是上面的屬性通過代理的方法傳回
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
           

文/阿聰o(簡書作者)

原文連結:http://www.jianshu.com/p/b0d03c40fd65

著作權歸作者所有,轉載請聯系作者獲得授權,并标注“簡書作者”。