0

I have a UICollectionView with a footer supplementary view. The way the collectionView works, it is possible to push the supplementary view offscreen when a new cell is inserted. The cells can have different horizontal sizing.

If two large cells are inserted and the supplementary view is pushed off of the screen, inserting a small cell between the two larger cells causes a crash.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:
atIndexPath (UICollectionElementKindCell,<NSIndexPath: 0xc00000000000000e> 
{length = 1, path = 0}) was not retrieved by calling 
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil ((null))'

I have determined that the crash occurs because the supplementary view is nil. It is nil because in

 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
   viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

I check to see what kind of element has been handed to the method. When the app crashes, the kind is UICollectionElementKindCell, for which I have no valid response.

So, my question is twofold :
1) Why is viewForSupplementaryElementOfKind being called with a kind that is not associated with my supplementary view? I expect that the UICollectionView should only try to get supplementary view information from this method.

2) How can this crash be avoided?

eugarps
  • 11
  • 3
  • you can return any view from as it, but you're already using footer view, so maybe try dequing it – Misha Oct 04 '16 at 17:31

1 Answers1

0

It is unclear why, but this crash was resolved by overriding the method

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

and returning true as well as removing the call to invalidateLayout that was in the insertion methods.

eugarps
  • 11
  • 3