9

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.

Mihai Popa
  • 862
  • 1
  • 8
  • 24

1 Answers1

5

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?

Community
  • 1
  • 1
Tom Kraina
  • 3,159
  • 1
  • 31
  • 53