有关UICollectionviewController的一些问题

一般我们使用UICollectionView 会直接使用UICollectionview,初始化传入一个flowLayout(可以是UICollectionviewFlowLayout ,也可以是自定义布局)

 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
     
 UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
如果你在自定义使用自定义的UICollectionviewController 。用来做引导页,做collection view的转场动画,等等。

你在viewDidLoad中按往常的写法,使用自定义的布局来初始化uicollectionview。会报错:错误信息UICollectionView must be initialized with a non-nil layout parameter'

意思明显是需要一个layout对象来初始化,但是在viewdidload 中有使用自定义的布局了 

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

    flowLayout.delegate = self;

    collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];

    collection.backgroundColor = [UIColor whiteColor];

    collection.delegate=self;

    collection.dataSource=self;

    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    [self.view addSubview: self.collectionView];

其实在自定义的UICollectionviewController中 初始化就需要传入一个 layout 参数。。

当我们使用[[UICollectionViewController alloc] init];初始化操作时 就需要带入自定义的flow layout 

可以在自定义的.m 中重写init方法,类似

- (instancetype)init

{

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

    flowLayout.delegate = self;

    return [super initWithCollectionViewLayout:flowLayout];

}

这样创建就可以带上自带的布局样式了,此外 需要将self.collectionview 设置为当前显示的collection view。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值