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"];
}
And in the .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:
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];
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…