UICollectionViewCell的四种创建方式

  • 方式一:纯代码创建
#import "ViewController.h"
#define kScreenW  [UIScreen mainScreen].bounds.size.width
#define kScreenH  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //列距
    flowLayout.minimumInteritemSpacing = 30;
    //行距
    flowLayout.minimumLineSpacing = 40;
    //item大大小
    flowLayout.itemSize = CGSizeMake((kScreenW-60)/3, 200);
    //初始化
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 24, kScreenW, kScreenH) collectionViewLayout:flowLayout];
    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    //设置背景颜色
    collectionView.backgroundColor = [UIColor whiteColor];
    //添加视图显示
    [self.view addSubview:collectionView];

 //注册时用UICollectionViewCell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

然后实现两个必须实现的代理方法

//设置个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;
}

//创建item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;

}

  • 方式二:自定义一个继承UICollectionViewCell的类来创建
#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kScreenW  [UIScreen mainScreen].bounds.size.width
#define kScreenH  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)NSArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

    flowLayout.minimumInteritemSpacing = 30;
    flowLayout.minimumLineSpacing = 40;
    flowLayout.itemSize = CGSizeMake((kScreenW-60)/3, 200);

    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 24, kScreenW, kScreenH) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];

//注意:注册时必须用自定义的类
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    _dataArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}

实现代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.name = self.dataArray[indexPath.item];

    return cell;

}

自定义MyCollectionViewCell.h

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic,copy)NSString *name;
@end

MyCollectionViewCell.m文件

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(void)setName:(NSString *)name{

    _name = name;

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 50)];
    label.text = _name;
    label.font = [UIFont systemFontOfSize:22];
    self.backgroundColor = [UIColor orangeColor];
    [self addSubview:label];
}

@end

  • 方式三:xib创建

#import "ViewController.h"
#define kScreenW  [UIScreen mainScreen].bounds.size.width
#define kScreenH  [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

    flowLayout.minimumInteritemSpacing = 30;
    flowLayout.minimumLineSpacing = 40;
    flowLayout.itemSize = CGSizeMake((kScreenW-60)/3, 200);

    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 24, kScreenW, kScreenH) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];

 //注意:注册时必须要用xib注册

    [collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

}

实现代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

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



    return cell;

}

xib文件:

这里写图片描述

这里写图片描述


  • 方式四:故事版创建
#import "ViewController.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.minimumLineSpacing = 30;
    self.flowLayout.itemSize = CGSizeMake(120, 200);

    //注意:用故事版创建时,无须注册

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

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


}

Main.storyboard

这里写图片描述

这里写图片描述


总结:这四种方式总体来看,代码大部分相同,只有那么一点点的差别。但是,如果没有搞清思路的话,就很容易混淆,导致出错哈,差异的部分我已经标注出来哦,相信大家能很快掌握这四种创建方式!如果感觉这篇文章不错的话,就顶一下吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值