控制台错误:UICollectionView必须使用非nil布局参数初始化。

时间:2023-01-22 17:55:56

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.

我是UICollectionView新手,我正在学习Youtube上找到的教程,但我遇到了一个我搞不懂的错误。

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"];

    }

And in the .h:

. 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'

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

我不使用故事板,自定义制作集合视图,你可以在这里看到:

控制台错误:UICollectionView必须使用非nil布局参数初始化。

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

有人知道我为什么会犯这个错误吗?一切都是受欢迎的!

edit:

编辑:

- (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];

}

2 个解决方案

#1


5  

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

注册uicollectionviewcell视图类时发生错误。要解决这一问题,请在您的代码中如下:

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

#2


2  

Register Collection cell in viewDidLoad method ::

viewDidLoad方法中的寄存器收集单元:

[self.collView registerClass:[mineCell class] forCellWithReuseIdentifier:@"cvCell"];

Try this after above line:

在上面一行之后试试这个:

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

[self.collView setCollectionViewLayout:flow];

Here, mineCell is my custom collectionViewCell or you can use [UICollectionViewCell class]directly.

在这里,mineCell是我的自定义collectionViewCell,或者您可以直接使用[UICollectionViewCell类]。

Thanks.

谢谢。

#1


5  

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

注册uicollectionviewcell视图类时发生错误。要解决这一问题,请在您的代码中如下:

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

#2


2  

Register Collection cell in viewDidLoad method ::

viewDidLoad方法中的寄存器收集单元:

[self.collView registerClass:[mineCell class] forCellWithReuseIdentifier:@"cvCell"];

Try this after above line:

在上面一行之后试试这个:

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

[self.collView setCollectionViewLayout:flow];

Here, mineCell is my custom collectionViewCell or you can use [UICollectionViewCell class]directly.

在这里,mineCell是我的自定义collectionViewCell,或者您可以直接使用[UICollectionViewCell类]。

Thanks.

谢谢。