我是 UICollectionView
的新手,我正在按照我在 Youtube 上找到的教程进行操作,但我遇到了一个我无法弄清楚的错误。
当我使用此代码运行应用时:
-(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;
在控制台中我收到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
我没有使用 Storyboard,而是自定义了 CollectionView,您可以在此处看到:
有人知道我为什么会收到此错误吗?欢迎一切!
编辑:
- (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 View 类时出错。要解决此问题,请在您的代码中添加以下行:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
关于iphone - 控制台错误 : UICollectionView must be initialized with a non-nil layout parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270058/