UICollectionView的使用

實現垂直方向的單列表,使用UITableView;如果須要構建橫向滑動列表、多行多列布局,使用UICollectionView+UICollectionViewFlowLayout搭建;更復雜的佈局,則可使用UICollectionView+自定義Layout來實現。佈局

UICollectionView工做流程:atom

當UICollectionView顯示內容時,先從數據源獲取cell交給UICollectionView。再從UICollectionViewLayout獲取對應的layout attributes(佈局屬性)。最後,根據每一個cell對應的layout attributes(佈局屬性)來對cell進行佈局,生成最終的界面。而用戶交互的時候,都是經過Delegate來進行交互。固然,上面只是佈局cell,可是UICollectionView內部還有Supplementary View和Decoration View,也能夠對其進行佈局。spa

1、視圖

UICollectionView上面顯示內容的視圖有三種Cell視圖、Supplementary View和Decoration View。代理

Cell視圖orm

CollectionView中主要的內容都是由它展現的,它是從數據源對象獲取的。對象

Supplementary Viewblog

它展現了每一組當中的信息,與cell相似,它是從數據源方法當中獲取的,可是與cell不一樣的是,它並非強制須要的。例如flow layout當中的headers和footers就是可選的Supplementary View。ci

Decoration View工作流

這個視圖是一個裝飾視圖,它沒有什麼功能性,它不跟數據源有任何關係,它徹底屬於layout對象。it

2、數據源和代理方法

一、註冊cell或者Supplementary View使其重用

在使用數據源返回cell或者Supplementary View給collectionView以前,咱們必須先要註冊,用來進行重用。

registerClass: forCellWithReuseIdentifier:
registerNib: forCellWithReuseIdentifier:
registerClass: forSupplementaryViewOfKind: withReuseIdentifier:
registerNib: forSupplementaryViewOfKind: withReuseIdentifier:

前兩個方法是註冊cell,後兩個方法是註冊supplementary view。

在數據源方法當中返回cell或者Supplementary view的方法當中經過dequeueReusableCellWithReuseIdentifier:forIndexPath: 

或dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法獲取cell或者Supplementary View。

二、數據源方法

數據源方法與UITableView相似,主要有:

numberOfSectionsInCollectionView:
collectionView: numberOfItemsInSection:
collectionView: cellForItemAtIndexPath:
collectionView: viewForSupplementaryElementOfKind: atIndexPath:

 與UITableView不一樣的是多加了返回Supplementary view數據源方法。

三、代理方法

數據源爲UICollectionView提供數據相關的內容,而代理則主要負責用戶交互、與數據無關的視圖外形。主要分紅兩部分:

一、經過調用代理方法,管理視圖的選中、高亮

collectionView:shouldDeselectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:

二、長按cell,顯示編輯菜單 與UITableView不一樣,用戶長按cell時,UICollectionView能夠顯示編輯菜單。這個編輯菜單能夠用來剪切、複製和粘貼cell。不過,要顯示這個編輯菜單,代理對象必須實現下面三個方法:

collectionView:shouldShowMenuForItemAtIndexPath:
collectionView:canPerformAction:forItemAtIndexPath:withSender:
collectionView:performAction:forItemAtIndexPath:withSender:

對於指定要編輯的cell,collectionView:shouldShowMenuForItemAtIndexPath:方法須要返回YES

collectionView:canPerformAction:forItemAtIndexPath:withSender: 方法中,對於剪切、複製、粘貼三種action至少有一個返回YES。其實,編輯菜單是有不少種action的,可是對於UICollectionView來講,它僅僅支持的剪切、複製、粘貼三個,因此說這個代理方法至少支持這三種的一種。 剪切、複製、粘貼的方法名是: cut: copy: paste:

當實現上面的代理方法了,用戶就能夠長按cell顯示出編輯菜單,而後選擇對應的action,從而就會回調delegate的collectionView:performAction:forItemAtIndexPath:withSender: 方法去作對應的事情。

當咱們想控制編輯菜單僅僅顯示覆制和粘貼時,咱們就能夠在collectionView:canPerformAction:forItemAtIndexPath:withSender:方法中進行操做,具體請見下面代碼:

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
  if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"]) {
    return YES;
    }
    return NO;
}

3、UICollectionViewLayout

UICollectionViewLayout是經過UICollectionViewLayoutAttributes類來管理cell、Supplementary View和Decoration View的位置、transform、alpha、hidden等等。 

UICollectionViewLayout這個類只是一個基類,咱們給UICollectionView使用的都是它的子類。系統爲咱們提供了一個最經常使用的layout爲UICollectionViewFlowLayout,咱們可使用它製做grid view。當UICollectionViewLayout知足不了咱們的需求時,咱們能夠子類化UICollectionViewLayout或者自定義layout。

UICollectionViewFlowLayout內部經常使用的屬性:

//同一組當中,垂直方向:行與行之間的間距;水平方向:列與列之間的間距
@property (nonatomic) CGFloat minimumLineSpacing;
//垂直方向:同一行中的cell之間的間距;水平方向:同一列中,cell與cell之間的間距
@property (nonatomic) CGFloat minimumInteritemSpacing;
//每一個cell統一尺寸
@property (nonatomic) CGSize itemSize;
//滑動反向,默認滑動方向是垂直方向滑動
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;
//每一組頭視圖的尺寸。若是是垂直方向滑動,則只有高起做用;若是是水平方向滑動,則只有寬起做用。
@property (nonatomic) CGSize headerReferenceSize;
//每一組尾部視圖的尺寸。若是是垂直方向滑動,則只有高起做用;若是是水平方向滑動,則只有寬起做用。
@property (nonatomic) CGSize footerReferenceSize;
//每一組的內容縮進
@property (nonatomic) UIEdgeInsets sectionInset;

注:UICollectionViewFlowLayout內部的屬性都是用來統一設置,如果統一設置沒法知足需求,能夠實現UICollectionViewDelegateFlowLayout代理方法,進行對應的設置。

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
//每一個cell的尺寸
- (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; 
//垂直方向:同一行中cell之間的間距;水平方向:同一列中cell的間距
- (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;
相關文章
相關標籤/搜索