- 方式一:纯代码创建
#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