Reputation: 13
I am creating a page in my app where a segmented control shows two different collectionviews. The data for the CVs comes from an API which contains two different length arrays.
My problem is that the first Collection View has more items in the array than the second, and the second collectionview is iterating through cellAtIndexPath more times than required and hence returning an error * Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
Can anyone help me please?
Thanks
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
DataStore *ds = [DataStore sharedDataStore];
if (self.cvActive) {
return ds.arLikesActive.count;
}
else {
return ds.arLikesExpired.count;
}
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DataStore *ds = [DataStore sharedDataStore];
if (collectionView == self.cvActive) {
CollectionViewCellLikedActive *cell = (CollectionViewCellLikedActive *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
self.dProductActive = ds.arLikesActive[indexPath.item];
cell.ivActiveProduct.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dProductActive[@"ImageURL"]]]];
return cell;
}
else {
CollectionViewCellLikedWhereAreTheyNow *cell = (CollectionViewCellLikedWhereAreTheyNow *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
self.dProductInactive = ds.arLikesExpired[indexPath.item];
cell.ivWhereAreTheyNowProduct.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dProductInactive[@"ImageURL"]]]];
return cell;
}
Upvotes: 0
Views: 206
Reputation: 104082
I think the problem is your if statement in the numberOfItemsInSection method,
if (self.cvActive) {
Isn't this always going to evaluate to true? It should look just like the one you have in cellForItemAtIndexPath,
if (collectionView == self.cvActive) {
Upvotes: 1