控制台错误:必须使用非零布局参数初始化UICollectionView [英] Console error: UICollectionView must be initialized with a non-nil layout parameter

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

问题描述

我是 UICollectionView 的新手,我正在关注我在Youtube上找到的教程,但我遇到了一个我无法弄清楚的错误。

I'm new to UICollectionView and I'm following a tutorial that I found on Youtube, but i'm stuck on an error I can't figure out.

当我使用此代码运行应用程序时:

When I run the app with this code:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return 1;

    }

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        return [self.array count];

    }

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

        CollectionCell *aCell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

        aCell.title.text = self.array[indexPath.row];

        return aCell;

    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

    }

并且在.h:

@property (strong, nonatomic) NSArray *array;

在控制台中我收到以下错误:

In the console I receive the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

我没有使用故事板,并自定义了CollectionView你可以在这里看到:

Im NOT using storyboard, and custom maked the CollectionView what you can see here:

有没有人有任何想法为什么我得到这个错误?欢迎一切!

Does anyone have any ideas why i'm getting this error? Everything is welcome!

编辑:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.array = @[@"First", @"Second", @"Thirth", @"Fourth"];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setItemSize:CGSizeMake(60, 60)];
    [flow setScrollDirection:UICollectionViewScrollDirectionVertical];

    [self.collectionView setCollectionViewLayout:flow];

}


推荐答案

它是注册uicollectionviewcell视图类时出错。要解决,请在代码中添加以下行:

it is an error while registering the uicollectionviewcell view class. To resolve put the below line in your code:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];

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

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