本文轉(zhuǎn)自iOS大神破船在devdiv論壇的帖子,原貼地址iOS6新特征:UICollectionView介紹.
1.1 Collection View元素
1.2 數(shù)據(jù)模型與交互
1.3 內(nèi)容的顯示
關(guān)鍵字:
UICollectionView
UICollectionViewDataSource
UICollectionViewDelegate
UICollectionViewCell
UICollectionViewLayout
UICollectionViewFlowLayout
UICollectionViewLayoutAttributes
全家福:
UICollectionView, UITableView, NSCollectionView
- 不直接等效于NSCollectionView
- 也不替代UITableView----親兄弟
為什么要使用Collection Views呢?
- 可以高度定制內(nèi)容的展現(xiàn)
- 管理數(shù)據(jù)最佳的做法
- 即使是處理大量數(shù)據(jù),也非常的高效
我們先來感性的認(rèn)識(shí)一下Collection Views,下面這幅圖就是用Collection Views實(shí)現(xiàn)的一個(gè)照片墻顯示。
1.1 Collection View的元素
從上圖中,我們可以看出Collection View的整體構(gòu)成元素,共有三個(gè)要素,分別如下:
- Cells(單元格)
- Supplementary Views(補(bǔ)充的view,相當(dāng)于TableView的頁眉和頁腳)
- Decoration Views(裝飾View,用于裝飾整個(gè)Collection View的)
我們可以分解一下這個(gè)照片墻,來描述上面的三個(gè)元素都對(duì)應(yīng)什么內(nèi)容。
Cells如下圖,即每一張圖片
Supplementary Views如下圖右邊白色的文字部分
Decoration Views如下圖
最終,三個(gè)元素,就構(gòu)成了照片墻,下面是元素構(gòu)成圖
1.2 數(shù)據(jù)模型與交互
數(shù)據(jù)模型(數(shù)據(jù)提供者UICollectionViewDataSource
)
UICollectionViewDataSource
是一個(gè)代理,主要用于向Collection View提供數(shù)據(jù)。
UICollectionViewDataSource
的主要功能:
- Section數(shù)目
- Section里面有多少item
- 提供Cell和supplementary view設(shè)置
下面我們依次講解這些功能。
1、numberOfSectionsInCollectionView:
下圖中有多少個(gè)sections呢?
答案是2個(gè)。即我們?cè)趎umberOfSectionsInCollectionView:方法中返回2即可。
2、collectionView:numberOfItemsInSection:
上圖圖中section 0中有多少個(gè)items呢?
答案是4個(gè)。即我們?cè)赾ollectionView:numberOfItemsInSection:方法中對(duì)應(yīng)的section 0返回4即可。
3、collectionView:cellForItemAtIndexPath:
上圖中section 0 item 0位置處應(yīng)該顯示什么內(nèi)容呢?
答案是使用方法collectionView:cellForItemAtIndexPath:返回一個(gè)cell,類似下圖中的一個(gè)view。
4、Cell和View的重用
下面通過5個(gè)步驟,來演示Cell和View的重用
1)如下圖,左邊是Collection View,右邊是Cell和View的重用隊(duì)列,剛開始,左邊的數(shù)據(jù)顯示內(nèi)容,右邊的重用隊(duì)列還沒有數(shù)據(jù)。
2)再看下圖,當(dāng)用戶顯示出了Collection View下面的內(nèi)容后,Collection View中之前的一些Cell和View就已經(jīng)不再被顯示了,這時(shí),系統(tǒng)是如何處理呢?
3)看這里,系統(tǒng)會(huì)把不用的Cell和View添加到重用隊(duì)列中,以備后面使用。
4)如何再次被使用呢,請(qǐng)看下圖,當(dāng)用戶繼續(xù)往下看內(nèi)容的時(shí)候,系統(tǒng)就會(huì)提供隊(duì)列中存在的Cell和View供使用。
5)最終使用效果如下圖
5、iOS6中,Cell重用改善
在iOS6中,我們可以更加方便的使用Cell,系統(tǒng)總是為我們初始化Cell。我們可以直接使用。只需要簡(jiǎn)單的按照兩步走即可:
1) 必須使用下面的方法進(jìn)行Cell類的注冊(cè):
- (void)registerClass:forCellWithReuseIdentifier:
- (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
- (void)registerNib:forCellWithReuseIdentifier:
- (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
2) 從隊(duì)列中取出一個(gè)Cell,具體方法如下:
- (id)dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
下面我們通過實(shí)際的代碼,來演示具體如何進(jìn)行Cell的重用
第一步:在collection view中進(jìn)行設(shè)置(Cell類的注冊(cè))
// In collection view setup...
[collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@”MY_CELL_ID”]
第二步:在下面的函數(shù)中,從隊(duì)列中取出一個(gè)cell即可。并且再也不用對(duì)cell進(jìn)行空值判斷,以做額外的初始化操作。Cell的一切初始化工作都由系統(tǒng)為我們做好了。我們只需要對(duì)cell進(jìn)行一些賦值等操作即可。
- (UICollectionView*)collectionView:(UICollectionView*)cv
cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@”MY_CELL_ID”];
if (!cell)
{
// Well, nothing really. Never again
}
// Configure the cell's content
cell.imageView.image = ...
return cell;
}
交互(UICollectionViewDelegate)
UICollectionViewDelegate的主要功能:
- 控制cell的高亮
- 控制cell的選擇
- 在cell上支持菜單操作,如下圖
選擇和高亮在iOS中都有所改進(jìn),高亮和flow的精確定位都非常好控制。
下面列出了常用的相關(guān)方法,開發(fā)者可以參考sdk的幫助文檔,進(jìn)行詳細(xì)了解。
1) 管理cell的高亮
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
這個(gè)方法collectionView:shouldHighlightItemAtIndexPath:
的效果如下圖所示:注意右邊selected和highlighted的值。
這個(gè)方法collectionView: didHighlightItemAtIndexPath:
的效果如下圖所示:注意右邊selected和highlighted的值。
2) 管理cell的選擇
collectionView:shouldSelectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:shouldDeselectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldSelectItemAtIndexPath:
的效果如下圖
下面兩個(gè)方法
collectionView:didUnhighlightItemAtIndexPath:
、
collectionView:didSelectItemAtIndexPath:
的效果如下圖所示:
1.3 內(nèi)容的顯示
UICollectionViewCell Styles,iOS6中沒有預(yù)定義cell的Style。
Collection View跟蹤cell的選擇和高亮。
通過設(shè)置cell的highlight和selection屬性(包含子視圖)
如果進(jìn)行了相關(guān)配置,這可以切換background view
和selected background view
.
我們來按順序看下面四幅圖。開發(fā)者可以自行領(lǐng)悟規(guī)律。
下圖是UICollectionView相關(guān)的類圖,從圖中我們可以看到
- UICollectionView繼承自UIScrollView,
- 尊循UICollectionViewDelegate和UICollectionViewDataSource兩個(gè)協(xié)議
- 管理UICollectionViewCell
圖中貌似還缺點(diǎn)東西,什么呢?對(duì)了,就是缺少Layout。我們需要Layout對(duì)cell和其它的view進(jìn)行布局。再看下圖,圖中多了UICollectionViewLayout和UICollectionViewFlowLayout。
下面我們對(duì)UICollectionViewLayout進(jìn)行介紹.
使用自己的layout(UICollectionViewLayout)
UICollectionViewLayout是一個(gè)抽象基類,你需要繼承自他,來為collection view生成layout信息。Layout對(duì)象的作用是決定cells,Supplementary views和Decoration views在collection view中的布局位置。
你需要計(jì)算如下view的layout屬性:
- Cells
- Supplementary views
- Decoration views
系統(tǒng)也為我們定義了layout屬性,即UICollectionViewLayoutAttributes,它主要包括如下內(nèi)容:
- 位置
- 大小
- 透明度
- ZIndex
- 轉(zhuǎn)場(chǎng)
如下面的兩個(gè)圖是collection view的layout。