在iOS開發(fā)中,與列表展示相關(guān)的,更多的是使用UITableView,幾乎每個App都離不開它。對比與UITableView,UICollectionView的使用比較繁瑣,所以UICollectionView的使用不是很常見。但UICollectionView也有自身的特點,比如橫向布局,多行或者多列(瀑布流)展示。Apple也推薦了一些應(yīng)用UICollectionView典型場景--相冊、書庫。
1.UICollectionView的創(chuàng)建
與UITableView類似,UICollectionView也可以通過控件的形式創(chuàng)建,其展示的UICollectionViewCell也可以是XIB的形式,這里更好的理解,使用代碼創(chuàng)建。
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
UICollectionView的創(chuàng)建過程基本和UITableView類似,alloc后init,設(shè)置dataSource代理和delegate代理,并添加到當(dāng)前view上。不同于UITableView初始化時一般要指定UITableViewStyle,UICollectionView初始化時指定的是UICollectionViewLayout對象(一般都是使用它的子類UICollectionViewFlowLayout)。這個對象保存了關(guān)于UICollectionView的布局信息。其主要屬性有
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirection
@property (nonatomic) UIEdgeInsets sectionInset;

圖為縱向滾動的UICollectionView
解釋一下含義
scrollDirection 方向
默認(rèn)縱向
itemSize 即一個Item的大小,藍(lán)色線指示的大小
在UITableView中,Cell一般的寬度是屏幕的寬度,默認(rèn)高度是44,我們常常是用heightForRowAtIndexPath來確定Cell的大小。
minimumLineSpacing 紅色線之間的距離
//The minimum spacing to use between lines of items in the grid.
因為UICollectionView可以一行顯示多個或者一列顯示多個,這個屬性用來指示行與行之間的最小距離(縱向),或者列與列之間的最小距離(橫向)。無論橫向或者縱向,都可以滾動顯示所有內(nèi)容,所以這個屬性可以單獨設(shè)置。
注意:這里定義UICollectionView的方向建立在設(shè)備在Portrait方向,即正常方向。如果考慮設(shè)備轉(zhuǎn)換方向,那么UICollectionView的所有方向都可以看為是縱向。
minimumInteritemSpacing 黃色線之間的距離
//The minimum spacing to use between items in the same row.
用來指示同行(縱向)或者同列(橫向)的item之間最小距離。在縱向滾動時,屏幕寬度固定,橫向滾動時,屏幕的高度固定,故該屬性的設(shè)置需要考慮itemSize。當(dāng)設(shè)置過大時,布局就會與預(yù)期不一致。如在上圖的情況下,如果該屬性設(shè)置過大,導(dǎo)致一行無法放置2個Item,其結(jié)果就是每行只有一個Item。
sectionInset 每一個 section 與周邊的間距
2.代理協(xié)議
代理協(xié)議有3個
UICollectionViewDelegateFlowLayout
UICollectionViewDelegate
UICollectionViewDataSource
UICollectionViewDelegateFlowLayout對應(yīng)于UICollectionViewFlowLayout的屬性設(shè)置,屬性是全局一致的,如果需要特殊設(shè)置(瀑布流等),就可以使用協(xié)議的方式來對每個對象進(jìn)行布局。主要方法如下
- (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;
UICollectionViewDataSource協(xié)議與UITaleViewDataSource協(xié)議類似,常用協(xié)議如下
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
分別對應(yīng)Section多少個Item,展示的UICollectionViewCell和UICollectionView多少個Section。
CollectionView也使用了重用機制,可以
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
自行判斷為空后再創(chuàng)建Cell,或者用
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
來注冊Cell。
UICollectionViewDelegate協(xié)議與UITaleViewDelegate協(xié)議類似,常用協(xié)議如下
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
分別對應(yīng)Item的選中和取消選中操作。