简体   繁体   中英

SVPullToRefresh crash with UICollectionView and iOS 7

I have this collection view which I want to add SVPullToRefresh's Infinite Scroll feature.

The collection view is set with 1 section and n (variable) cells.

I included the feature:

  __weak MyClass *weakSelf = self;

  [_collection addInfiniteScrollingWithActionHandler:^ {
    [weakSelf loadNextPage];
  }];

And the collection view shows perfectly as it should. On iOS 6, the method -loadNextPage is called normally and the app runs with no problems. On the other hand, on iOS 7, when the VC is about to call the method, the app crashes with the following error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x1ca8fc50> {length = 2, path = 1 - 0}'

I've been trying to find what exactly is the piece of code, or which part in the code flow the exception appears, but I can't find it.

Edit: added some code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  ProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  Product *product = [_productList objectAtIndex:[indexPath row]];
  [cell setName:[product name]];
  [cell setImage:[product imageURL]];
  [cell setPrice:[product formattedPrice]];
  [cell setDistance:[product formattedDistance]];
  return cell;
}

Try

- (NSInteger)numberOfItemsInSection:(NSInteger)section {
    [_collection.collectionViewLayout invalidateLayout]
}

What we are doing here is, when we have more data to be rendered, we invalidate the current collection view layout as it will not work with new data and the collection view will layout itself with new available data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM