Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewFlowLayout with self sizing cells crash after reloadData

I have a custom UICollectionViewFlowLayout with non-zero estimatedItemSize for cells. I add and remove cells inside the collectionView reloading the data and sometimes I get a crash when I try to scroll to a cell that has just been removed/added.

Using backtrace tool, I get the following result:

frame #9: 0x0000000185c8385c CoreFoundation-[__NSArrayM objectAtIndex:] + 264 frame #10: 0x000000018aeabb18 UIKit-[_UIFlowLayoutSection setSize:forItemAtIndexPath:] + 708 frame #11: 0x000000018ae7cc5c UIKit-[UICollectionViewFlowLayout invalidationContextForPreferredLayoutAttributes:withOriginalAttributes:] + 156 frame #12: 0x000000018ae646c4 UIKit-[UICollectionView _checkForPreferredAttributesInView:originalAttributes:] + 212 frame #13: 0x000000018a844968 UIKit-[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 776

I have a guess that my flow layout could cache the cells.

UPDATE: My issue appears on iOS 8.3, but not on previous versions, 8.2 included.

like image 802
Mihai Popa Avatar asked Apr 20 '15 12:04

Mihai Popa


1 Answers

Don't use self-sizing cell with non-zero estimatedItemSize. It's a trap.

Implement a UICollectionViewDelegateFlowLayout's method collectionView(collectionView:collectionViewLayout:indexPath:) where you calculate the size of the cell by calling systemLayoutSizeFittingSize on a custom instance of the cell.

You can find more detailed info in this question: UICollectionView - dynamic cell height?

like image 105
Tom Kraina Avatar answered Oct 13 '22 17:10

Tom Kraina