IOS---UICOLLECTIONVIEW詳解和常用API翻譯

來源:互聯網
上載者:User

標籤:

IOS---UICOLLECTIONVIEW詳解和常用API翻譯UICollectionView
  • 1.必須要設定布局參數
  • 2.註冊cell
用法類似於UITableView 類。自動實現重用,必須註冊初始化。使用UICollectionView必須實現 UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout這三個協議。Collection View的構成,我們能看到的有三個部分:
  • Cells
  • Supplementary Views 追加視圖 (類似Header或者Footer)
  • Decoration Views 裝飾視圖 (用作背景展示)
一、UICollectionViewLayout
  • UICollectionView的精髓
  • Layout決定了UICollectionView是如何顯示在介面上的。在展示之前,一般需要產生合適的UICollectionViewLayout子類對象,並將其賦予CollectionView的collectionViewLayout屬性。
1.UICollectionViewFlowLayout
  • 最簡單也是最常用的預設layout對象,???UICollectionViewFlowLayout。Flow Layout簡單說是一個直線對齊的layout,
常用屬性
// 行間距,也可以通過collectionView: layout:minimumLineSpacingForSectionAtIndex:@property (nonatomic) CGFloat minimumLineSpacing;// 設定cell之間的間距@property (nonatomic) CGFloat minimumInteritemSpacing;// 定義每一個item的大小。通過設定itemSize可以全域地改變所有cell的尺寸,如果想要對某個cell制定尺寸,//可以使用-collectionView:layout:sizeForItemAtIndexPath:方法。@property (nonatomic) CGSize itemSize;@property (nonatomic) CGSize estimatedItemSize// 滾動方向,預設是水平// UICollectionViewScrollDirectionVertical// UICollectionViewScrollDirectionHorizontal@property (nonatomic) UICollectionViewScrollDirection scrollDirection;// 根據滾動方向不同,header和footer的高和寬中只有一個會起作用。// 垂直滾動時section間寬度為該尺寸的高,而水平滾動時為寬度起作用,@property (nonatomic) CGSize headerReferenceSize;@property (nonatomic) CGSize footerReferenceSize;// 組間距 縮排@property (nonatomic) UIEdgeInsets sectionInset;
UICollectionViewDelegateFlowLayout代理方法
// 設定指定Cell的尺寸- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;// 設定collectionView(指定區)的邊距- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;// 設定指定區內Cell的最小行距,也可以直接設定UICollectionViewFlowLayout的minimumLineSpacing屬性- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section// 設定指定區內Cell的最小間距,也可以直接設定UICollectionViewFlowLayout的minimumInteritemSpacing屬性- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
2.UICollectionViewLayoutAttributes
  • 一個非常重要的類,
屬性列表
// 邊框@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
3.自訂的UICollectionViewLayout
  • UICollectionViewLayout的功能為向UICollectionView提供布局資訊.
  • 繼承UICollectionViewLayout類。
重寫方法
// 返回collectionView的內容的尺寸-(CGSize)collectionViewContentSize// 返回rect中的所有的元素的布局屬性/*返回的是包含UICollectionViewLayoutAttributes的NSArrayUICollectionViewLayoutAttributes可以是cell,追加視圖或裝飾視圖的資訊,通過不同的UICollectionViewLayoutAttributes初始化方法可以得到不同類型的UICollectionViewLayoutAttributes:*/-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect// 返回對應於indexPath的位置的cell的布局屬性-(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath//返回對應於indexPath的位置的追加視圖的布局屬性,如果沒有追加視圖可不重載-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath// 返回對應於indexPath的位置的裝飾視圖的布局屬性,如果沒有裝飾視圖可不重載-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath// 當邊界發生改變時,是否應該重新整理布局。如果YES則在邊界變化(一般是scroll到其他地方)時,將重新計算需要的布局資訊。-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  • 另外需要瞭解的是,在初始化一個UICollectionViewLayout執行個體後,會有一系列準備方法被自動調用,以保證layout執行個體的正確。

  • 首先,-(void)prepareLayout將被調用,
    • 預設下該方法什麼沒做,但是在自己的子類實現中
    • ,一般在該方法中設定一些必要的layout的結構和初始需要的參數等。
  • 之後,-(CGSize) collectionViewContentSize將被調用,
    • 以確定collection應該佔據的尺寸。注意這裡的尺寸不是指可視部分的尺寸,而應該是所有內容所佔的尺寸。
    • collectionView的本質是一個scrollView,因此需要這個尺寸來配置滾動行為。
二、UICollectionView1.UICollectionViewDataSource①section的數量 ?-numberOfSectionsInCollection:②某個section裡有多少個item ?-collectionView:numberOfItemsInSection:③對於某個位置應該顯示什麼樣的cell ?-collectionView:cellForItemAtIndexPath:2.UICollectionViewDelegate
// 當指定indexPath處的item被選擇時觸發- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath//返回這個UICollectionView是否可以被選擇-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;// 下面是三個和高亮有關的方法:- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath/*事件的處理順序如下:1.手指按下2.shouldHighlightItemAtIndexPath (如果返回YES則向下執行,否則執行到這裡為止)3.didHighlightItemAtIndexPath (高亮)4.手指鬆開5.didUnhighlightItemAtIndexPath (取消高亮)6.shouldSelectItemAtIndexPath (如果返回YES則向下執行,否則執行到這裡為止)7.didSelectItemAtIndexPath (執行選擇事件)

IOS---UICOLLECTIONVIEW詳解和常用API翻譯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.