【轉】UICollectionView使用介紹

UICollectionView 使用介紹



UICollectionView 使用介紹
1.1. Collection View
全家福:
UICollectionView, UITableView, NSCollectionView
不直接等效於NSCollectionView
也不替代UITableView----親兄弟
爲何要使用Collection Views呢?
能夠高度定製內容的展示
管理數據最佳的作法
即便是處理大量數據,也很是的高效
咱們先來感性的認識一下Collection Views,下面這幅圖就是用Collection Views實現的一個照片牆顯示。

1.1.1 Collection View 元素




從上圖中,咱們能夠看出Collection View的總體構成元素,共有三個要素,分別以下
Cells(單元格)
Supplementary Views(補充的view,至關於TableView的頁眉和頁腳)
Decoration Views(裝飾View,用於裝飾整個CollectionView的)
咱們能夠分解一下這個照片牆,來描述上面的三個元素都對應什麼內容
Cells
以下圖,即每一張圖片

SupplementaryViews
以下圖右邊白色的文字部分



Decoration Views
以下圖

最終,三個元素,就構成了照片牆,下面是元素構成圖



1.1.2 數據模型與交互
數據模型(數據提供者UICollectionViewDataSource)
UICollectionViewDataSource
是一個代理,主要用於向Collection View提供數據。
UICollectionViewDataSource
的主要功能:
Section數目
Section裏面有多少item
提供Cellsupplementary view設置
下面咱們依次講解這些功能。
1
numberOfSectionsInCollectionView:
下圖中有多少個sections呢?
答案是2個。即咱們在numberOfSectionsInCollectionView:方法中返回2便可。

2
collectionView:numberOfItemsInSection:
下圖中section0中有多少個items呢?
答案是4個。即咱們在collectionView:numberOfItemsInSection:方法中對應的section 0返回4便可。
3
collectionView:cellForItemAtIndexPath:
下圖中section0 item 0位置處應該顯示什麼內容呢?
答案是使用方法collectionView:cellForItemAtIndexPath:返回一個cell,相似下圖中的一個view

4
CellView的重用
下面經過5個步驟,來演示CellView的重用
1
)以下圖,左邊是Collection View,右邊是CellView的重用隊列,剛開始,左邊的數據顯示內容,右邊的重用隊列尚未數據。

2
)再看下圖,當用戶顯示出了Collection View下面的內容後,Collection View中以前的一些CellView就已經再也不被顯示了,這是,系統是如何處理呢?

3
)看這裏,系統會把不用的CellView添加到重用隊列中,以備後面使用。

4
)如何再次被使用呢,請看下圖,當用戶繼續往下看內容的時候,系統就會提供隊列中存在的CellView供使用。

5
)最終使用效果以下圖



5
iOS6中,Cell重用改善
iOS6中,咱們能夠更加方便的使用Cell,系統老是爲咱們初始化Cell。咱們能夠直接使用。只須要簡單的按照兩步走便可:
1
 必須使用下面的方法進行Cell類的註冊:
- (void)registerClass:forCellWithReuseIdentifier:
- (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
- (void)registerNib:forCellWithReuseIdentifier:
- (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
2
 從隊列中取出一個Cell,具體方法以下:
-(id)dequeueReusableCellWithReuseIdentifier:forIndexPath:
-(id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
下面咱們經過實際的代碼,來演示具體如何進行Cell的重用
第一步:在collection view中進行設置(Cell類的註冊)
// In collectionview setup...
[collectionView registerClass:[MyCellclass]
forCellWithReuseIdentifier:@"MY_CELL_ID"]
第二步:在下面的函數中,從隊列中取出一個cell便可。而且不再用對cell進行空值判斷,以作額外的初始化操做。Cell的一切初始化工做都由系統爲咱們作好了。咱們只須要對cell進行一些賦值等操做便可。
-(UICollectionView*)collectionView:(UICollectionView*)cv
cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCell *cell =[cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL_ID"];
if (!cell) {
// Well, nothingreally. Never again
}
// Configure thecell's content
cell.imageView.image= ...
return cell;
}
交互(UICollectionViewDelegate)
UICollectionViewDelegate
的主要功能:
控制cell的高亮
控制cell的選擇
cell上支持菜單操做,以下圖
                          

選擇和高亮在iOS中都有所改進,高亮和flow的精肯定位都很是好控制。
下面列出了經常使用的相關方法,開發者能夠參考sdk的幫助文檔,進行詳細瞭解。
1
 管理cell的高亮
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
這個方法collectionView:shouldHighlightItemAtIndexPath:的效果以下圖所示:注意右邊selectedhighlighted的值。

這個方法collectionView:didHighlightItemAtIndexPath:的效果以下圖所示:注意右邊selectedhighlighted的值。

2
 管理cell的選擇
collectionView:shouldSelectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:shouldDeselectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldSelectItemAtIndexPath:
的效果以下圖

下面兩個方法
collectionView:didUnhighlightItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
的效果以下圖所示:

1.1.3 內容的顯示
UICollectionViewCell Styles
iOS6
中沒有預約義cellStyle
Collection View
跟蹤cell的選擇和高亮
      
經過設置cellhighlightselection屬性(包含子視圖)
      
若是進行了相關配置,這能夠切換background viewselected background view
咱們來按順序看下面四幅圖。開發者能夠自行領悟規律。








下圖是UICollectionView相關的類圖,從圖中咱們能夠看到
l UICollectionView
繼承自UIScrollView
l
 尊循UICollectionViewDelegateUICollectionViewDataSource兩個協議
l
 管理UICollectionViewCell

圖中貌似還缺點東西,什麼呢?對了,就是缺乏Layout。咱們須要Layoutcell和其它的view進行佈局。再看下圖,圖中多了UICollectionViewLayoutUICollectionViewFlowLayout

下面咱們對UICollectionViewLayout進行介紹
使用本身的layoutUICollectionViewLayout
UICollectionViewLayout
是一個抽象基類,你須要繼承自他,來爲collection view生成layout信息。Layout對象的做用是決定cellsSupplementary viewsDecorationviewscollection view中的佈局位置。
你須要計算以下viewlayout屬性
Cells
Supplementary views
Decoration views
系統也爲咱們定義了layout屬性,即UICollectionViewLayoutAttributes,它主要包括以下內容:
位置
大小
透明度
ZIndex
轉場
以下面的兩個圖是collection viewlayout



技術博客http://www.cnblogs.com/ChenYilong/ 
新浪微博http://weibo.com/luohanchenyilong 

1.2 Flow Layout
1.2.1 核心概念
UICollectionViewFlowLayout
是一個具體的layout對象,用來把item佈局在網格中,而且可選頁眉和頁腳。在collection view中的items,能夠從一行或者一列flow至下一行或者下一列(行或者列取決於滾動的方向)。每行都會根據狀況,包含儘量多的CellsCells能夠是相同的尺寸,也能夠是不一樣的尺寸。
下面是Flow Layout的一些特性
面向線性佈局
可配置爲網格
一組lines
具備頁眉和頁腳

1.2.2 自定義 Flow Layout
Flow Layout能夠定製的主要功能以下
Item size
Line spacing
►Inter cell spacing
Scrolling direction
Header and footer size
Section Inset
下面咱們來詳細介紹這些可定製的功能。
這裏須要對經過全局和delegate定製進行說明一下:
幾乎全部的定製屬性都是在UICollectionViewFlowLayout中,delegate其實是collection viewdelegateUICollectionViewDelegateFlowLayout只是對UICollectionViewDelegate進行了一些擴展。
Item size(每一個item的大小)
1 能夠進行全局配置,以下代碼
@property(CGSize)itemSize
layout.itemSize= CGSizeMake(30,20);

2
 也能夠經過delegate對每個item進行配置,以下代碼
collectionView:layout:sizeForItemAt
IndexPath:
{
return…;
}

效果以下圖所示


Line spacing(每行的間距)
1 能夠進行全局配置,以下屬性
@property(CGFloat) minimumLineSpacing

2
 也能夠經過delegate對每個section進行配置,以下代碼
ollectionView:layout:minimumLineSpacingForSectionAtIndex:

請按順序看下面的三個圖






Inter cell spacing(每行內部cell item的間距)
1 能夠進行全局配置,以下屬性
@property(CGFloat) minimumInteritemSpacing

2
 也能夠經過delegate對每個section進行配置,以下代碼
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

看看下面的兩個圖,藍色是實際的item間距,綠色的是最小item間距。




Scrolling direction(滾動方向)
設置scrollDirection屬性便可。兩個值以下
UICollectionViewScrollDirectionVertical
UICollectionViewScrollDirectionHorizontal
主要做用:
定義了Flow Layout的基本行爲
 控制頁眉頁腳的維度

UICollectionViewScrollDirectionVertical
效果以下圖所示


UICollectionViewScrollDirectionHorizontal
效果以下圖所示


Header and footer size(頁眉和頁腳大小)
下面是頁眉和頁腳的一些解釋。
 也就是supplementary views
 經過數據源的方法來提供內容,以下
-collectionView:viewForSupplementaryElementOfKind:atIndexPath:

 兩種常量(類型)
UICollectionElementKindSectionHeader
UICollectionElementKindSectionFooter

 一樣須要註冊一個類並從隊列中取出view
- registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
-registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:

頁眉和頁腳的size配置方式:
1
)能夠全局配置,以下屬性
@property(CGSize) headerReferenceSize
@property(CGSize) footerReferenceSize

2
)也能夠經過delegate對每個section進行配置,以下代碼
collectionView:layout:referenceSizeForHeaderInSection:
collectionView:layout:referenceSizeForFooterInSection:
頁眉頁腳的屬性以下圖


當垂直的時候,須要設置Height,以下圖


當水平的時候,須要設置Width,以下圖


Section Inset
咱們先經過兩個圖來看看Sections Insets是怎麼回事




從上面的兩個圖中,咱們大概知道了,Section Inset就是某個sectioncell的邊界範圍。

SectionInset
的配置方式一樣有兩種
1
 經過全局配置,以下屬性
@propertyUIEdgeInsets sectionInset;

2
 也經過delegate對每個section進行配置,以下函數
- (UIEdgeInsets)collectionView:layout:insetForSectionAtIndex:

DEMO





技術博客http://www.cnblogs.com/ChenYilong/ 
新浪微博http://weibo.com/luohanchenyilong


© chenyilong. Powered by Postach.io
相關文章
相關標籤/搜索