1.创建继承CollectionViewCell的文件为:CECollectionViewCell
2.在CE.h中写入属性
@property (nonatomic, strong) UIImageView *imgView; // 图片
@property (nonatomic, strong) UILabel *label; // 内容
3.在CE.m中写入自定义
-(instancetype)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){
[self addSubview:self.imgView];
[self addSubview:self.label];
}
return self;
}
-(UIImageView *)imgView{
if(!_imgView){
_imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 260)];
_imgView.layer.masksToBounds = YES;
}
return _imgView;
}
-(UILabel *)label{
if(!_label){
// 设置 frame
_label = [[UILabel alloc] initWithFrame:CGRectMake(5, 205, 180, 20)];
// 设置 字体
_label.font = [UIFont systemFontOfSize:15];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
}
return _label;
}
4.在controller.m中调用
#import "ViewController.h"
#import "CECollectionViewCell.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
UICollectionView *coll;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
[self wg];
[self.view addSubview:coll];
// self.view.backgroundColor=[UIColor whiteColor];
}
//网格
-(void)wg{
//创建流水
UICollectionViewFlowLayout *ls=[[UICollectionViewFlowLayout alloc]init];
//网格大小
ls.itemSize=CGSizeMake(150, 250);
//行间距
ls.minimumLineSpacing=1;
//列间距
ls.minimumInteritemSpacing=1;
//分区间距
ls.sectionInset=UIEdgeInsetsMake(0, 0, 20, 10);
//滚动方向
ls.scrollDirection=UICollectionViewScrollDirectionVertical;
//创建网格
coll=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:ls];
coll.backgroundColor=[UIColor whiteColor];
//代理i
coll.delegate=self;
coll.dataSource=self;
//注册
[coll registerClass:[CECollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 4;
}
- (CECollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CECollectionViewCell *cell=[coll dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor redColor];
cell.label.text=@"请你点击我,我就会给你看视频";
cell.label.textColor=[UIColor yellowColor];
return cell;
}