我是新手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'
我没有使用情节提要,并且自定义了 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];
}