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

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

问题描述

我通过代码添加了 UICollectionView

现在,应用程序崩溃并显示消息: UICollectionView必须使用非初始化-nil布局参数

您有什么想法修复它吗?

CollectionCell 是自定义类 UICollectionViewCell

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 的Apple文档,你会发现唯一的初始化程序是 initWithFrame:collectionViewLayout:。此外,在初始化程序的参数中,您将看到:

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


frame

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

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 ,并且必须将其传递给非零 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天全站免登陆