UICollectionView:必须用非零布局参数初始化 [英] UICollectionView: must be initialized with a non-nil layout parameter

查看:42
本文介绍了UICollectionView:必须用非零布局参数初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过代码添加了 UICollectionView.
现在,应用程序崩溃并显示以下消息:UICollectionView 必须使用非零布局参数初始化.
你有什么解决办法吗?
CollectionCellUICollectionViewCell 的自定义类.

I added UICollectionView by code.
Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter.
Do you have any idea to fix it?
CollectionCell is custom class of UICollectionViewCell.

@property (nonatomic, strong) UICollectionView* collectionView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView = [[UICollectionView alloc]init];
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell"];
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 100);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:flowLayout];

    self.collectionView.frame = CGRectMake(0, 60, 320, 500);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.eventCollection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell* cell = [self.eventCollection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.label.text = [NSString stringWithFormat:@"%d", indexPath.item];
    return cell;
}

推荐答案

崩溃非常明确地告诉你出了什么问题:

The crash is telling you pretty explicitly what is going wrong:

UICollectionView 必须使用非零布局参数初始化.

UICollectionView must be initialized with a non-nil layout parameter.

如果您检查 UICollectionView 的文档,你会发现唯一的初始化器是 initWithFrame:collectionViewLayout:.此外,在该初始化程序的参数中,您会看到:

If you check the documentation for UICollectionView, you'll find that the only initializer is initWithFrame:collectionViewLayout:. Further, in the parameters for that initializer, you'll see:

框架

集合视图的框架矩形,以磅为单位.框架的原点是相对于您计划在其中添加它的超级视图.此帧在初始化期间传递给超类.

The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.

布局

用于组织项目的布局对象.集合视图存储对指定对象的强引用.不能为零.

The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.

我已经加粗了重要的部分.您必须使用 initWithFrame:collectionViewLayout: 来初始化您的 UICollectionView,并且您必须向其传递一个非 nil UICollectionViewLayout 对象.

I've bolded the important part. You must use initWithFrame:collectionViewLayout: to initialize your UICollectionView, and you must pass it a non-nil UICollectionViewLayout object.

因此,解决此问题的一种方法是简单地更改您执行的初始化顺序:

One way to fix this, then, would be to simply change the order of initialization you do:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

请注意,在上面的示例中,我假设您想使用 self.view.frame 作为 self.collectionView 的框架.如果不是这种情况,请插入您想要的任何框架.

Note that in the above example, I've assumed you want to use self.view.frame as the frame of self.collectionView. If that's not the case, insert whatever frame you want instead.

这篇关于UICollectionView:必须用非零布局参数初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆