THSpringyCollectionView icon indicating copy to clipboard operation
THSpringyCollectionView copied to clipboard

Error when reloading the collection view data

Open kgn opened this issue 11 years ago • 2 comments

UICollectionView recieved layout attributes for a cell with an index path that does not exist

kgn avatar Dec 22 '13 01:12 kgn

It seems like for optimization purpose, cached behaviors and visibleIndexPaths are not reseted in prepareLayout. So if your datasource change and you call reloadData, there will be a crash: your error will surely happen when you call reloadData and your previous number of items was "bigger" than the new one.

You could add a reset method to your layout class that empties _animator and _visibleIndexPaths arrays. Then you call [yourCollectionViewLayout reset] just before reloadData each time you want to refresh your collection view... I'm pretty sure it will work but if you have to reload the data often then maybe you should reimplement a layout more appropriate to your needs. Check this post to understand how have been thought the optimizations that cause the problem: http://www.objc.io/issue-5/collection-views-and-uidynamics.html

Hope it helps

aurelienp avatar Feb 06 '14 23:02 aurelienp

I get this error when using UICollectionViewFlowLayout. Any ideas how it could have happened?

fatuhoku avatar May 04 '14 22:05 fatuhoku