UICollectionViewCell 3种创建方法

本文介绍了在iOS开发中创建UICollectionViewCell的3种方法:1) 纯代码创建,包括自定义cell的实现;2) 使用xib文件创建,需要进行注册并设置内容;3) 利用storyboard拖拽创建,通过设置唯一标识符和代理方法完成。每种方法都有其适用场景,开发者可根据需求选择合适的方式。

UICollectionViewCell 3种创建方法
有错误的地方,希望指出,(^__^)

1、纯代码
(1)同一个文件创建,代码如下,先看代码,我在下面讲解引入的自定义cell

//--------------纯代码实现------------------

    //1.布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //a) 设置大小(即每个小视图的大小)
    flowLayout.itemSize = CGSizeMake(50, 50);
    //b)设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //c) 设置行间距
    flowLayout.minimumLineSpacing = 20;
    //d) 设置水平间距
    flowLayout.minimumInteritemSpacing = 10;

    //2.计算每个集合视图的(大视图的)frame
    CGRect frame = CGRectMake(0, 20, kScreenWidth, kScreenHeight-20);

    //3.创建集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    //a) 设置背景颜色
    collectionView.backgroundColor = [UIColor cyanColor];
    //b) 设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    //c) 设置隐藏滑动条
    collectionView.showsVerticalScrollIndicator = NO;
    //d) 添加到视图
    [self.view addSubview:collectionView];

    //单元格注册
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];

}

#pragma mark - 实现代理方法
//1.设置创建多少个items
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 50;
}
//2.返回视图内容
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    //2.创建cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    //3.设置背景颜色
    cell.backgroundColor = [UIColor whiteColor];

    //4.创建单元格被选中显示的视图
    UIView *view = [[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = view;

    return cell;
}

//3.自定义单元格尺寸(可设置,可不设置)
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat width = arc4random()%10*10+5;
    CGFloat height = arc4random()%10*10+5;
    return CGSizeMake(width, height);
}
(2) 不同文件的,即需要引入自定义的collectionViewCell
那么注册单元格时,引入自定义单元格的.h文件,代码为
//d) 注册
    [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];

这样我们只需要在返回cell的代理方法中写两句代码

//2.返回items
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];

    return cell;
}

在自定义的cell文件中设置cell的内容,可以复写initWithFrame(CGRect)frame的方法,我试了一下init方法,好像不行。

2、xib文件创建
创建一个xib文件,给里面添加collectionviewcell,然后设置一些内容(自己随意),然后注册的代码为
注意的是:在加载xib文件之前,要实例化,也就是绑定一个继承UICollectionViewCell的类,不然加载会出现问题

//4.单元格注册
    [collectionView registerNib:[UINib nibWithNibName:@"CollectionView" bundle:nil] forCellWithReuseIdentifier:@"identifier"];

然后代理方法中,和第一种方法里面是一样的,只是不用再设置cell的内容。
3、storyboard方法创建
也就是手动在storyboard中拖进一个viewController,然后在上面再拖进一个collectioView,自己随意添加内容,要记得手动连线设置代理,为collectionViewCell设置identifier(唯一标示符),将flowLayout和collectionView连线到要实现的文件中,storyboard方法创建的不用注册单元格,代理方法代码如下
这里的identifier就是在storyboard中设置的唯一标示符,必须一致。

//2.返回cell
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    //使用storyboard创建的不用注册
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    return cell;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值