tristanhimmelman / THSpringyCollectionView

A memory and CPU efficient implementation of a collection view with cells that bounce around like they do in the iOS 7 messages app
MIT License
726 stars 92 forks source link

Error when reloading the collection view data #4

Open kgn opened 10 years ago

kgn commented 10 years ago

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

aurelienp commented 10 years ago

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

fatuhoku commented 10 years ago

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