流水布局UICollectionView初级的自定义

上节讲的是基本的流水布局但是没讲自定义,此节简单的讲下自定义

//定义每个uicollectionview的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(80, 90);

}

上述方法是给collectionview添加大小,width和heith

我们都自定义跟uitableview一样,uicollectionview自定义继承与uicollectionviewcell

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        self.button = [UIButton buttonWithType:UIButtonTypeCustom];

        self.button.frame = CGRectMake(0, 0, 100, 50);

        self.button.backgroundColor = [UIColor orangeColor];

        [self.button setTitle:@"测试" forState:0];

        self.button.layer.cornerRadius = 5;

        self.button.layer.masksToBounds = YES;

        [self.contentView addSubview:self.button];

    }

    return self;

}

此处的frame很多不晓得怎么定义,其实就是对一个collectionview进行设置好比:cgsizemake(100,100);我们就对这100,100进行布局(不晓得我这样说算不算对的,不要误导大家了),假如要上半部分图片,下半部分btn,那么image的frame就是(0.0.100.70),btn的frame就是(0.0.100.30);

下面谈下一步

我们在uiviewcontroller里面初始化uicollectionview里面有个uicollectionviewcell

[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

还有在我们必须实现的一个方法里也有个uicollectionviewcell

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

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


将这两处跟uitableview自定义一样的替换成继承uicollectionviewcell的名字commmot+r就可以看到效果了,若只替换掉一处那么系统会崩溃


赋值跟tableview的自定义一样,此处就不多说了,基本的的自定义就是这样了,假如要换成其他模式就是自定义层:uicollectionviewflowlayout,这个在接下来的几篇ccdn会写,以后我会附带上截图和录制动画,以后还望大神多多指点小蒲




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值