UICollectionView

UICollectionView簡(jiǎn)介

UICollectionView是一種應(yīng)用廣泛的數(shù)據(jù)展示方式,是iOS6.0以后引進(jìn)的。在各大app都有廣泛的引用。

QQ音樂熱搜
下廚房集市頁
花瓣的發(fā)現(xiàn)

UICollectionView主要包括了下面幾部分:

  1. UICollectionView-內(nèi)容顯示的主視圖,類似于UITableView。
  2. UICollectionViewCell-類似于UITableViewCell在UITableView中。這些cell組成了視圖中的內(nèi)容。
  3. Supplementary Views-如果你除了cell之外,還有信息需要展示,你可以使用supplementary views。通常被用作section的header或者footer
  4. Decoration View-裝飾視圖,用來提升UICollectionView的背景視覺效果的。

除了上面這些可見的組成部分,UICollectionView還有用來布局內(nèi)容的不可見組成部分。

  1. UICollectionViewLayout- 用來處理cell在屏幕上的布局。使用一系列的代理方法在UICollectionView上放置cell,而且布局效果是可以在運(yùn)行時(shí),隨時(shí)改變的。

創(chuàng)建UICollectionView的常用方法

  • UICollectionView
    1.創(chuàng)建cell以及header,footer
    使用代碼創(chuàng)建
    - registerClass:forCellWithReuseIdentifier:
    - registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
    使用xib創(chuàng)建
    - registerNib:forCellWithReuseIdentifier:
    - registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
    復(fù)用cell
    - dequeueReusableCellWithReuseIdentifier:forIndexPath:
    - dequeueReusableSupplementaryViewOfKind:
    :withReuseIdentifier:forIndexPath:

      2.獲取Collection View中的Item及位置
      - indexPathForItemAtPoint:
      - indexPathsForVisibleItems
      - indexPathForCell:
      - cellForItemAtIndexPath:
    
      3.獲取Collection View的狀態(tài)
      - numberOfSections
      - numberOfItemsInSection:
    
  • UICollectionViewDelegate
    1.處理選擇的Cells
    - collectionView:shouldSelectItemAtIndexPath:
    - collectionView:didSelectItemAtIndexPath:
    - collectionView:shouldDeselectItemAtIndexPath:
    - collectionView:didDeselectItemAtIndexPath:

      2.處理Cells的高亮
      - collectionView:shouldHighlightItemAtIndexPath:
      - collectionView:didHighlightItemAtIndexPath:
      - collectionView:didUnhighlightItemAtIndexPath:
    
  • UICollectionViewDataSource
    1.獲取Section和Item的數(shù)量
    - collectionView:numberOfItemsInSection:
    - numberOfSectionsInCollectionView:

      2.獲取Items的視圖
      - collectionView:cellForItemAtIndexPath:
      - collectionView:viewForSupplementaryElementOfKind:
        atIndexPath:
    

會(huì)變魔術(shù)的UICollectionViewLayout

UICollectionView之所以可以有千變?nèi)f化的樣式,各種酷帥屌炸天的動(dòng)畫效果,都要?dú)w功于UICollectionViewLayout,玩好的UICollectionViewLayout可以實(shí)現(xiàn)你想要的各種效果。

UICollectionViewLayout會(huì)對(duì)屏幕上的cells進(jìn)行組織布局。
UICollectionViewLayoutAttributes描述了具體如何布局cells, supplementary views, decoration views,具體包括以下屬性:

  • frame -view在collection view的坐標(biāo)系統(tǒng)中的具體位置
  • center -view在collection view的坐標(biāo)系統(tǒng)中的中心
  • size -view的大小
  • transform3D -用來旋轉(zhuǎn),縮放的屬性
  • zIndex -子視圖的層級(jí)
  • hidden -view是否隱藏

布局的生命周期:


來自iOS 6 By Tutotials
  • 藍(lán)框代表colletion view在布局時(shí)使用的方法
  • 橙框代表colletion view的狀態(tài)
  • 綠框代表collection view的使用者調(diào)用的方法

collection view首先調(diào)用prepareLayout ,在其中設(shè)置布局需要的內(nèi)部數(shù)據(jù)結(jié)構(gòu)。這時(shí)collection view已經(jīng)弄清了要展示的數(shù)據(jù),但還不知道如何布局,所以你可以在prepareLayout中使用numberOfSections或numberOfItemsInSection:這些方法。然后調(diào)用collectionViewContentSize獲取content size進(jìn)行布局。之后調(diào)用layoutAttributesForElementsInRect
對(duì)可以在屏幕中顯示的部分進(jìn)行布局。layout必須返回屏幕中所有元素的布局屬性,包括UICollectionViewCell或者Supplementary Views或者Decoration Views。這時(shí),collectionView了解了足夠多的信息去布局cells。當(dāng)scrollview滑動(dòng)時(shí),collection view會(huì)繼續(xù)調(diào)用layoutAttributesForElementsInRect:去決定新的可見元素的布局。

當(dāng)下面三種情況發(fā)生時(shí),又會(huì)調(diào)用布局的相關(guān)方法。

  • invalidateLayout -所有布局重新布局
  • insertItemAtIndexPathsdeleteItemsAtIndexPaths
    -這個(gè)會(huì)發(fā)生在用戶添加或者移除collection view中的
    item時(shí),這時(shí)會(huì)調(diào)用prepareForCollectionViewUpdate: **
    finalizeAnimatedBoundsChange:**
  • 當(dāng)collection view的約束改變時(shí) - 調(diào)用
    prepareForAnimatedBoundsChange
    finalizeAnimatedBoundsChange

由于UICollectionViewLayout是抽象類,所以在布局的時(shí)候可以使用Apple提供的UICollectionViewFlowLayout或者自定義Layout

UICollectionViewFlowLayout

這是一個(gè)典型的柵格布局,就像上圖所以的下廚房的集市頁布局。它有以下的一些屬性:

    scrollDrirection:決定了scroll view的滾動(dòng)方向
    minimumLineSpacing:決定了每行的間隔
    minimumIteriItemSpacing:決定了Item之間的間隔
    itemSize:決定了Item的大小
    sectionInset:section四周的邊距
    headerReferenceSize:header的大小
    footerReferenceSize:footer的大小
自定義UICollectionViewLayout:

自定義UICollectionViewLayout一般是繼承UICollectionViewLayout類。重載下列方法:

    -(void)prepareLayout:設(shè)定一些初始化參數(shù)。
    -(CGSize)collectionViewContentSize:設(shè)定Collection View的尺寸
    -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect:返回屏幕中所有元素的布局屬性
    -(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的cell的布局屬性
    -(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath:返回indexPath位置的補(bǔ)充視圖的布局屬性
    -(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的裝飾視圖的布局屬性
    -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds:邊界改變時(shí),是否重新布局。

在下一篇中將介紹幾種常用的布局,如開篇的截圖所示,柵格網(wǎng)狀,靠左對(duì)齊,瀑布流的布局。雖然早已有相關(guān)輪子,但是作為初學(xué)者,模仿輪子,知道輪子的造法,還是很有必要的。

參考

1.http://onevcat.com/2012/06/introducing-collection-views/
2.《iOS 6 By Tutorials》
3.https://github.com/mokagio/UICollectionViewLeftAlignedLayout
4.https://github.com/ChenYilong/CollectionViewClassifyMenu
5.http://www.itdecent.cn/p/22adf62ea491

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • The UICollectionView class manages an ordered collection ...
    lixiaoshuai閱讀 867評(píng)論 0 0
  • 在創(chuàng)建自定義的layout之前,你需要知道UICollectionViewFlowLayout提供的很多特性已經(jīng) ...
    縱橫而樂閱讀 20,865評(píng)論 7 91
  • 什么是UICollectionView? UICollectionView是一種新的數(shù)據(jù)展示方式,簡(jiǎn)單來說可以把他...
    凌峰Mical閱讀 43,669評(píng)論 11 201
  • //聯(lián)系人:石虎 QQ: 1224614774昵稱:嗡嘛呢叭咪哄 什么是UICollectionView UICo...
    石虎132閱讀 3,610評(píng)論 0 15
  • 將一個(gè)數(shù)組轉(zhuǎn)化為一個(gè)List對(duì)象,這個(gè)方法會(huì)返回一個(gè)ArrayList類型的對(duì)象, 這個(gè)ArrayList類并非 ...
    Leocat閱讀 329評(píng)論 0 0

友情鏈接更多精彩內(nèi)容