2

I have collectionView with two custom layouts, if some action happens I'm removing two items from data source and trying to set different collection view

    [self.tabCollectionView setCollectionViewLayout:self.basicFlowLayout animated:NO];

I'm getting error:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionViewData.m:357
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xbcecd60> {length = 2, path = 0 - 4}'

However when I do that after removing only one item, animation works well. Also there is no problems on iOS 7. Struggling with this whole day and can't find proper solution to fix it. Any ideas?

ninja_iOS
  • 1,183
  • 1
  • 13
  • 20
  • check if this answer helps you [UICollectionView assertion error on stale data][1] [1]: http://stackoverflow.com/questions/18339030/uicollectionview-assertion-error-on-stale-data – Vaionixx Apr 15 '14 at 10:52
  • I was checking that, and it didn't help – ninja_iOS Apr 16 '14 at 09:45

1 Answers1

0

I think the problem is that you are making several changes in a short time to the collection view leaving its state inconsistent in some cases.

Try grouping all the insert/remove/change layout changes inside a performBatchUpdates:completion: call:

Discussion

You can use this method in cases where you want to make multiple changes to the collection view in one single animated operation, as opposed to in several separate animations. You might use this method to insert, delete, reload or move cells or use it to change the layout parameters associated with one or more cells. Use the blocked passed in the updates parameter to specify all of the operations you want to perform.

Deletes are processed before inserts in batch operations. This means the indexes for the insertions are processed relative to the indexes of the state before the batch operation, and the indexes for the deletions are processed relative to the indexes of the state after all the insertions in the batch operation.


As written above, animations are not suppressed, but I guess they get "merged" instead.

As for the exception you have to make sure to preserve the correspondence between your data model (what the dataSource returns) and the collection view:

  • Make sure that you have already removed the items from your data model before calling deleteItemsAtIndexPaths:.

  • Make sure you don't change the data model when changing the layout.

The timing can be tricky so again you should try to performBatchUpdates.

Rivera
  • 10,792
  • 3
  • 58
  • 102