0

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;

}
VickyS
  • 13
  • 1
  • Does cvActive point to a particular collection view, or is it a property that indicates which collection view is the currently active one? If the latter, how are you setting it? – rdelmar Jun 14 '14 at 16:02
  • cvActive points to a particular collection view: – VickyS Jun 14 '14 at 16:22

1 Answers1

1

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) {
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • amazing thank you!!!!! That's been bugging me for hours, and It was just a stupid little mistake! Thanks so much :) – VickyS Jun 14 '14 at 16:37