xcode 控制台错误:UICollectionView 必须使用非 nil 布局参数进行初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15270058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Console error: UICollectionView must be initialized with a non-nil layout parameter
提问by SDW
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:
我没有使用故事板,而是自定义了 CollectionView 您可以在这里看到的内容:
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];
}
回答by Saurabh Singh
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"];
回答by MS.
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 class]
直接使用。
Thanks.
谢谢。