UICollectionView簡單使用

1. UICollectionView

1.Cells
2.Supplementary Views 追加視圖 (類似Header或者Footer)
3.Decoration Views 裝飾視圖 (用作背景展示)

1.1三個代理 :

1.DataSource  提供數(shù)據(jù) 
2.Delegate  用戶交互 
3.CollectionViewLayout  布局 

2. UICollectionViewLayout

2.1 UICollectionViewLayoutAttributes類的介紹:

1一個cell對應(yīng)一個UICollectionViewLayoutAttributes對象
2.UICollectionViewLayoutAttributes對象決定了cell的擺設(shè)位置(frame)

@property (nonatomic) CGRect frame 
@property (nonatomic) CGPoint center @property (nonatomic) CGSize size 
@property (nonatomic) CATransform3D transform3D 
@property (nonatomic) CGFloat alpha 
@property (nonatomic) NSInteger zIndex @property (nonatomic, getter=isHidden) BOOL hidden

2.2 自定義的UICollectionViewLayout
需要重載在以下方法:

- (void)prepareLayout 準(zhǔn)備方法被自動調(diào)用,以保證layout實例的正確。 

- (CGSize)collectionViewContentSize 返回collectionView的內(nèi)容的尺寸 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 1.返回rect中的所有的元素的布局屬性 2.返回的是包含UICollectionViewLayoutAttributes的NSArray 3.UICollectionViewLayoutAttributes可以是cell,追加視圖或裝飾視圖的信息,通過不同的UICollectionViewLayoutAttributes初始化方法可以得到不同類型的UICollectionViewLayoutAttributes: 1)layoutAttributesForCellWithIndexPath: 2)layoutAttributesForSupplementaryViewOfKind:withIndexPath: 3)layoutAttributesForDecorationViewOfKind:withIndexPath: 

-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath 返回對應(yīng)于indexPath的位置的cell的布局屬性 

- (UICollectionViewLayoutAttributes )layoutAttributesForSupplementaryViewOfKind:(NSString )kind atIndexPath:(NSIndexPath *)indexPath 返回對應(yīng)于indexPath的位置的追加視圖的布局屬性,如果沒有追加視圖可不重載 

- (UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString)decorationViewKind atIndexPath:(NSIndexPath )indexPath 返回對應(yīng)于indexPath的位置的裝飾視圖的布局屬性,如果沒有裝飾視圖可不重載 

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds 當(dāng)邊界發(fā)生改變時,是否應(yīng)該刷新布局。如果YES則在邊界變化(一般是scroll到其他地方)時,將重新計算需要的布局信息。

2.3 調(diào)用順序

1)- (void)prepareLayout 設(shè)置layout的結(jié)構(gòu)和初始需要的參數(shù)等。

2) - (CGSize) collectionViewContentSize 確定collectionView的所有內(nèi)容的尺寸。

3)- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect初始的layout的外觀將由該方法返回的UICollectionViewLayoutAttributes來決定。4)在需要更新layout時,需要給當(dāng)前l(fā)ayout發(fā)送 1)-invalidateLayout, 該消息會立即返回,并且預(yù)約在下一個loop的時候刷新當(dāng)前l(fā)ayout 2)-prepareLayout, 3)依次再調(diào)用-collectionViewContentSize和-layoutAttributesForElementsInRect來生成更新后的布局。

繼承UICollectionViewLayout之后只需要重載幾個提供布局核心特性的方法,其他方法只需按情況重載即可,核心特性如下:

1 指定可滾動內(nèi)容區(qū)域的size

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

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

  • UICollectionView 是沒有類似于UITableview的整體表頭的,在注冊cell時,如果分區(qū)表頭,...
    雪_晟閱讀 975評論 0 1
  • 引 開發(fā)中我們最??吹降目赡苁潜硪晥DUITableView了,但其實還有一個視圖也很常見,特別是一些圖片、商品、視...
    Cloudox_閱讀 994評論 0 0
  • 1.Conversion Function 基本類型之間可以互相轉(zhuǎn)換,那么類類型呢?看下面代碼 輸出結(jié)果是4.6 ...
    rrreal閱讀 294評論 0 0
  • 01逆襲的迷茫 前幾天我有幸參加了一場企業(yè)內(nèi)部一般管理崗位的招聘面試。面試人員有來自基層崗位上出色的班長、專業(yè)對口...
    漪漣軒主閱讀 266評論 0 2
  • 三月,情未央,花開,傾城。 沿著三月的時光,我翻閱著曾經(jīng),那一份初相遇,晚春,初夏,那些濃濃的情懷,也便兀...
    一指流沙1閱讀 256評論 2 2

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