UICollectionViewCell 3种创建方法

来源:互联网 发布:变频布林线源码 编辑:程序博客网 时间:2024/06/05 19:18

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;}
0 0
原创粉丝点击