iOS6新特徵:UICollectionView介紹

 

iOS6新特徵:UICollectionView介紹

分類: ios
 

目錄(?)[+]ios

 

1.1. Collection Viewide

全家福:函數

UICollectionView, UITableView, NSCollectionView佈局

n   不直接等效於NSCollectionViewpost

n   也不替代UITableView----親兄弟spa

 

爲何要使用Collection Views呢?.net

n  能夠高度定製內容的展示代理

n  管理數據最佳的作法對象

n  即便是處理大量數據,也很是的高效blog

 

咱們先來感性的認識一下Collection Views,下面這幅圖就是用Collection Views實現的一個照片牆顯示。

 

1.1.1 Collection View 元素

 

 

 

 

從上圖中,咱們能夠看出Collection View的總體構成元素,共有三個要素,分別以下

n Cells(單元格)

n Supplementary Views(補充的view,至關於TableView的頁眉和頁腳)

n Decoration Views(裝飾View,用於裝飾整個CollectionView的)

 

咱們能夠分解一下這個照片牆,來描述上面的三個元素都對應什麼內容

Cells以下圖,即每一張圖片

 

 

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

 

 

Decoration Views以下圖

 

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

 

 

 

1.1.2 數據模型與交互

數據模型(數據提供者UICollectionViewDataSource)

UICollectionViewDataSource是一個代理,主要用於向Collection View提供數據。

UICollectionViewDataSource的主要功能:

n  Section數目

n  Section裏面有多少item

n  提供Cell和supplementary view設置

下面咱們依次講解這些功能。

 

一、numberOfSectionsInCollectionView:

下圖中有多少個sections呢?

答案是2個。即咱們在numberOfSectionsInCollectionView:方法中返回2便可。

 

 

二、collectionView:numberOfItemsInSection:

下圖中section0中有多少個items呢?

答案是4個。即咱們在collectionView:numberOfItemsInSection:方法中對應的section 0返回4便可。

 

 

三、collectionView:cellForItemAtIndexPath:

下圖中section0 item 0位置處應該顯示什麼內容呢?

答案是使用方法collectionView:cellForItemAtIndexPath:返回一個cell,相似下圖中的一個view。

 

四、Cell和View的重用

下面經過5個步驟,來演示Cell和View的重用

1)以下圖,左邊是Collection View,右邊是Cell和View的重用隊列,剛開始,左邊的數據顯示內容,右邊的重用隊列尚未數據。

 

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

 

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

 

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

 

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的主要功能:

n  控制cell的高亮

n  控制cell的選擇

n  在cell上支持菜單操做,以下圖

                            

選擇和高亮在iOS中都有所改進,高亮和flow的精肯定位都很是好控制。

下面列出了經常使用的相關方法,開發者能夠參考sdk的幫助文檔,進行詳細瞭解。

1) 管理cell的高亮

 

collectionView:shouldHighlightItemAtIndexPath:

collectionView:didHighlightItemAtIndexPath:

collectionView:didUnhighlightItemAtIndexPath:

 

這個方法collectionView:shouldHighlightItemAtIndexPath:的效果以下圖所示:注意右邊selected和highlighted的值。

 

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

 

2) 管理cell的選擇

 

collectionView:shouldSelectItemAtIndexPath:

collectionView:didSelectItemAtIndexPath:

collectionView:shouldDeselectItemAtIndexPath:

collectionView:didDeselectItemAtIndexPath:

 

 

collectionView:shouldSelectItemAtIndexPath:的效果以下圖

 

下面兩個方法

collectionView:didUnhighlightItemAtIndexPath:

collectionView:didSelectItemAtIndexPath:的效果以下圖所示:

 

1.1.3 內容的顯示

UICollectionViewCell Styles

iOS6中沒有預約義cell的Style

Collection View跟蹤cell的選擇和高亮

      經過設置cell的highlight和selection屬性(包含子視圖)

      若是進行了相關配置,這能夠切換background view和selected background view

 

咱們來按順序看下面四幅圖。開發者能夠自行領悟規律。

 

 

 

 

 

下圖是UICollectionView相關的類圖,從圖中咱們能夠看到

l UICollectionView繼承自UIScrollView,

l 尊循UICollectionViewDelegate和UICollectionViewDataSource兩個協議

l 管理UICollectionViewCell

 

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

 

下面咱們對UICollectionViewLayout進行介紹

使用本身的layout(UICollectionViewLayout)

UICollectionViewLayout是一個抽象基類,你須要繼承自他,來爲collection view生成layout信息。Layout對象的做用是決定cells,Supplementary views和Decorationviews在collection view中的佈局位置。

你須要計算以下view的layout屬性

n  Cells

n  Supplementary views

n  Decoration views

 

系統也爲咱們定義了layout屬性,即UICollectionViewLayoutAttributes,它主要包括以下內容:

n  位置

n  大小

n  透明度

n  ZIndex

n  轉場

 

以下面的兩個圖是collection view的layout。

 

1.2 Flow Layout

1.2.1 核心概念

UICollectionViewFlowLayout是一個具體的layout對象,用來把item佈局在網格中,而且可選頁眉和頁腳。在collection view中的items,能夠從一行或者一列flow至下一行或者下一列(行或者列取決於滾動的方向)。每行都會根據狀況,包含儘量多的Cells。Cells能夠是相同的尺寸,也能夠是不一樣的尺寸。

下面是Flow Layout的一些特性

n 面向線性佈局

n 可配置爲網格

n 一組lines

n 具備頁眉和頁腳

 

1.2.2 自定義 Flow Layout

Flow Layout能夠定製的主要功能以下

n Item size

n Line spacing

n Inter cell spacing

n Scrolling direction

n Header and footer size

n Section Inset

下面咱們來詳細介紹這些可定製的功能。

這裏須要對經過全局和delegate定製進行說明一下:

幾乎全部的定製屬性都是在UICollectionViewFlowLayout中,delegate其實是collection view的delegate。UICollectionViewDelegateFlowLayout只是對UICollectionViewDelegate進行了一些擴展。

Item size(每一個item的大小)

一、  能夠進行全局配置,以下代碼

@property(CGSize)itemSize

layout.itemSize= CGSizeMake(30,20);

 

二、  也能夠經過delegate對每個item進行配置,以下代碼

collectionView:layout:sizeForItemAt

IndexPath:

{

return...;

}

 

效果以下圖所示

 

Line spacing(每行的間距)

一、  能夠進行全局配置,以下屬性

@property(CGFloat) minimumLineSpacing

 

二、  也能夠經過delegate對每個section進行配置,以下代碼

ollectionView:layout:minimumLineSpacingForSectionAtIndex:

 

請按順序看下面的三個圖

 

 

 

Inter cell spacing(每行內部cell item的間距)

一、  能夠進行全局配置,以下屬性

@property(CGFloat) minimumInteritemSpacing

 

二、  也能夠經過delegate對每個section進行配置,以下代碼

collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

 

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

31.jpg

 

32.jpg

 

Scrolling direction(滾動方向)

設置scrollDirection屬性便可。兩個值以下

UICollectionViewScrollDirectionVertical

UICollectionViewScrollDirectionHorizontal

主要做用:

n  定義了Flow Layout的基本行爲

n  控制頁眉頁腳的維度

 

UICollectionViewScrollDirectionVertical效果以下圖所示

 

UICollectionViewScrollDirectionHorizontal效果以下圖所示

 

Header and footer size(頁眉和頁腳大小)

下面是頁眉和頁腳的一些解釋。

n 也就是supplementary views

n 經過數據源的方法來提供內容,以下

-collectionView:viewForSupplementaryElementOfKind:atIndexPath:

 

n 兩種常量(類型)

UICollectionElementKindSectionHeader

UICollectionElementKindSectionFooter

 

n 一樣須要註冊一個類並從隊列中取出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就是某個section中cell的邊界範圍。

 

SectionInset的配置方式一樣有兩種

一、  經過全局配置,以下屬性

@propertyUIEdgeInsets sectionInset;

 

二、  也經過delegate對每個section進行配置,以下函數

- (UIEdgeInsets)collectionView:layout:insetForSectionAtIndex:

 

DEMO

相關文章
相關標籤/搜索