UICollectionView的几种创建方法

关于创建UICollectionView的几种方法的总结
1、用storyBoard方式创建,在storyBoard中拖进去一个UICollectionView,然后将它与h文件连接,向UICollectionView中拖一个UICollectionViewCell,然后设置cell的显示格式,注意必须设置cell的resuseID(不是RestorationID),在m文件中不需要注册一个cell直接在代理方法中使用代码为:

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];

2、用UICollectionViewCell.xib创建cell,在视图控制器中注册cell,注意在这里必须要标明cell的重用id,不然后出现错误,标明重用ID时和上面内容一样。
主要代码如下:

//注册一个collectionView
    [_collectionView registerClass:[FirstCell class] forCellWithReuseIdentifier:@"list"];
    //重用单元格
    FirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list" forIndexPath:indexPath];

3、代码创建
主要代码如下:

//创建collcetionView
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height) collectionViewLayout:flowLayout];
    //注册一个collectionView
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    //重用单元格
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];

目前我理解的创建方式有这几种,以后会继续更新,谢谢支持!
自己创建的几种方式的代码链接为:
http://note.youdao.com/yws/public/redirect/share?id=bf117f4cad9b8ce0c73bfa3de78f2e1d&type=false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值