iOS开发之UICollectionViewController

1、概述

UICollectionView控件主要是用来做九宫格的,类似于android中的GridView控件。其用法与UITableView一样,首先要使控制器遵守数据源协议,再将控制器设置为UICollectionView的数据源。同样,控制器遵守了UICollectionView的代理后也可以实现代理方法等。

2、常用的数据源方法

设置UICollectionViewController一共有多少组:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView

*)collectionView;

设置每组有多少单元格:

- (NSInteger)collectionView:(UICollectionView *)collectionView

numberOfItemsInSection:(NSInteger)section;

设置每个单元格显示的内容:

- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

3、常用的代理方法

设置每个单元格点击事件:

- (void)collectionView:(UICollectionView *)collectionView

didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

MJProduct *p = self.products[indexPath.item];

    NSLog(@"点击了---%@", p.title);

}

4UICollectionViewController必须调用的方法

(1)注册cell(告诉collectionView将来创建怎样的cell)

[self.collectionView registerClass:[UICollectionViewCell class]

forCellWithReuseIdentifier:@"product"];

在调用- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;设置每个单元格显示的内容(UICollectionViewCell)之前必须注册cell,一般在viewDidLoad中调用上面方法注册cell。

例如:

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    // 1.注册cell(告诉collectionView将来创建怎样的cell)

    UINib *nib = [UINib nibWithNibName:@"MJProductCell" bundle:nil];

[self.collectionView registerNib:nib forCellWithReuseIdentifier:

MJProductCellID];//通过xid自定义的cell

    /*

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJProductCellID];//使用默认的UICollectionViewCell

*/

    // 2.设置collectionView的背景色

    self.collectionView.backgroundColor = [UIColor whiteColor];

}

 (2)从缓存池中取出cell

- (UICollectionViewCell *)collectionView:(UICollectionView

*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

UICollectionViewCell *cell =

[collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];

    return cell;

}

在从缓存池取UICollectionViewCell重用时,不用再判断缓存池是否为空了,它与UITableView不同的是UICollectionView在缓存池没有找到cell时会自动创建新的cell。

(3)重写init方法,创建布局参数

- (id)init

{

    // 1.流水布局

UICollectionViewFlowLayout *layout =

[[UICollectionViewFlowLayout alloc] init];

    // 2.每个cell的尺寸

    layout.itemSize = CGSizeMake(80, 80);

    // 3.设置cell之间的水平间距

    layout.minimumInteritemSpacing = 0;

    // 4.设置cell之间的垂直间距

    layout.minimumLineSpacing = 10;

    // 5.设置所有cell组成的一个整体与屏幕(ViewController)四周距离

layout.sectionInset =

UIEdgeInsetsMake(layout.minimumLineSpacing, 0, 0, 0);

    return [super initWithCollectionViewLayout:layout];

}

如果不创建布局参数程序会报错。一般在重写控制器的init方法中创建。

5UICollectionViewFlowLayout

UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示。

常见属性:

Cell的尺寸:

@property (nonatomic) CGSize itemSize;

cell之间的水平间距:

@property (nonatomic) CGFloat minimumInteritemSpacing;

cell之间的垂直间距:

@property (nonatomic) CGFloat minimumLineSpacing;

四周的内边距:

@property (nonatomic) UIEdgeInsets sectionInset;

原文地址:https://www.cnblogs.com/lifengfneg/p/4773959.html