天天看點

這裡總結一下collection cell的三種建立方式的相關步驟,原形cell,xib,代碼;還有對collection view的資料流flowlayout屬性與collectionview的頭底

這裡總結一下collection cell的三種建立方式的相關步驟,原形cell,xib,代碼;還有對collection view的資料流flowlayout屬性與collectionview的頭底部view的生成的總結。(讀者可根據需要到相關位置檢視)

一、原形cell

    1、在storyboard的collection view中拖拉一個系統cell, 設定重用标志。

    2、綁定一個繼承了UICollectionViewCell的自定義類,拉線,定義模型資料屬性之類的。

    3、controller中懶加載資料。

    4、使用資料源方法建立組,行,cell。

    5、在建立cell的datasource的方法中

    //注意這裡因為是原形cell,是以不需要判斷下面這行建立的cell是不是為空,每次建立先去緩存池找有沒有标志的cell,如果沒有就去找原形cell

    自定義類名 *cell = [collectionView dequeReuseIdentifier:@"app" forIndexPath:indexPath];

    cell.module屬性 = ...

    return cell;

二、xib建構collection cell(尤其要注意這裡要注冊cell)

    1、建立一個collection cell xib,添加自定義控件,添加限制。

    2、綁定一個繼承了UICollectionView cell的自定義類,拉線,定義模型資料屬性。

    3、controller中懶加載模型資料。

    4、注冊cell,說明從指定的xib檔案中建立

        建立Nib對象 參數:(1)需要加載的xib檔案名

                        (2)bundle目錄,如果是nil,則預設是mainBundle

    viewDidLoad中,UINib *nib = [UINib nibWithNibName:@"nib名字" bundle:nil];

    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"app"];

    綁定類 *cell = [[nib instanceWithOwner:nil options:nil] lastObject];

    可以建立一個臨時cell來設定資料流(後面會說)

    5、建立組,行,cell,其中cell的建立方法中:

綁定類 *cell = [collectionView dequeReuseableCellWithReuseIdentifier:@"app" forIndexPath:indexPath];

    cell.module屬性 = ...

    return cell;

三、代碼建構collectionView cell

    1、自定義一個cell類,繼承了UICollectionCell

        .h中,有資料模型及各種面向外面的屬性

        .m中,有類的擴充,裡面定義了各種空間的屬性

    1)控件初始化方法

    -(instancetype) initWithFrame:(CGRect)frame{

        self = [super initWithFrame:frame];

        return self;

    }

    2)給.h中的模型屬性添加setter方法

    3)layoutSubViews方法(每次collectionView發生變化都會調用一次這個方法,這個方法主要是對子控件做布局用的)

    -(void)layoutSubViews{

        [super layoutSubViews];

    }

    2、viewDidLoad中,建立collectionView對象與UICollectionFlowlayout對象

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame: self.view.bounds collectionViewLayout:layout];

    [self.view addSubView:collectionView];

    同時記住要注冊cell,

    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"app"];

    3、實作datasource的方法和上面xib的第5步一緻

    四、flowlayout,是collectionView特有的屬性,是描述cell在collectionview中布局的重要屬性,可以脫線綁定collectionview的綁定類中:

1、設定itemSize(用于設定cell的屬性)

self.flowlayout.itemSize = CGSizeMake(,);

2、設定組的内間距

self.flowlayout.sectionInset = UIEdgeInsetsMake(,,,);

3、設定行間距,與collectionview的滾動方向一緻

self.flowlayout.minimumLineSpacing = ...;

4、設定列間距

selfself.flowlayout.minimumLineInteritemSpacing = ...;

5、設定滾動方向,預設是豎直方向上的

self.flowlayout.scrollDirection = UICollectionViewScrollDirection...;

6、設定頭底部view的匡高,若豎直則寬無效,若水準,則高無效

self.flowlayout.header/footerReferenceSize = CGSizeMake(,);

7、讓組的頭部或底部固定在螢幕的四個可視的邊界:

self.flowlayout.sectionHeaders/FootersPinToVisibleBounds = BOOL;

    五、collectionview會自動建立它的頭底部view,當出現在可視化界面的時候,想到如自己建立的頭底部view的時候

    -(UICollectionViewReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

        NSString *kindType = @"header";

        if (kindType == UICollectionElementKindSectionFooter){

            kindType = @"footer";

        }

        UICollectionViewReusableView *view = [collectionView dequeReuseableSupplymentaryViewOfKind:kind WithReuseIdentifier:kindType forIndexPath:indexPath];

        return view;

    }