您的位置:首页 > 产品设计 > UI/UE

有关UICollectionviewController的一些问题

2016-06-22 16:06 344 查看
一般我们使用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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息