What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist ios ios

What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist


The crash was happening in the following situation:

We had a collection view controller that was presenting another view controller on top of it.

While the collection view controller was no longer visible, the following sequence of events was occasionally occurring in response to our app's back end requests.

  1. [UICollectionView insertItemsAtIndexPaths:] was called with 50items on the collection view of the hidden UICollectionViewController.
  2. [UICollectionView reloadData] was called on the hidden collection view.
  3. A short delay would occur.
  4. The number of items in the hidden collection view was set to a small number.
  5. [UICollectionView reloadData] was called again.
  6. The view controller was dismissed, revealing the hidden collection view controller.

The assertion failure in the internal UIKit class UICollectionViewData would happen at step 6.

So, the lesson is, try to avoid manipulating a collection view that is not visible on the screen.

Our workaround for this problem was to call [UICollectionView reloadSections:] instead of [UICollectionView reloadData] at key points.

We suspect that the effects of reloadData are deferred to some point in the future, and there are consequently subtle issues with how this may interact with other method calls like insertItemsAtIndexPaths, whereas reloadSections is handled immediately, leaving the collection view in a better state.

We think that we were not seeing this behavior until we started building our app for iOS 8.

Sleep well, my friends!


For me it was the UICollectionViewLayoutAttribute's array. I'm using it in a UICollectionViewLayout to store item's attribute.

I forgot to empty it in the prepareLayout method.

So the layoutAttributesForItemAtIndexPath was returning incorrect values for indexPath, which led to the same crash.

With just a removeAll on the array at the beginning of prepareLayout, it's working.


collectionViewLayout caches the attributes.In viewwillappear - Create a new instance of collectionViewLayout and assign it to collectionview.collectionViewLayoutIn this way all the cached attributes will purge before the reloadYour problem might be resolved. Worked for me, especially when you are using other collectionViewLayout libraries.