自定义网格步奏

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值