iOS 15 ONLY crash at -collectionView:viewForSupplementaryElementOfKind:atIndexPath:

THIS IS iOS 15 ONLY - THE SAME CODE WORKS FOR iOS 14.x USING XCODE 13.x, see below for details

When using background elements with UICollectionViewFlowLayout and UICollectionViewCompositionalLayout in iOS 15 the following happens on .reloadData(), .reconfigureItems(...), and .reloadItems(...) only (initial load is fine):

  • collectionView:viewForSupplementaryElementOfKind:atIndexPath: is called for background view
  • Background view is dequeued SUCCESSFULLY
  • viewForSupplementaryElementOfKind returns the view
  • App crashes post viewForSupplementaryElementOfKind (sometimes up to seconds later) saying viewForSupplementaryElementOfKind has returned null

I have verified:

  • viewForSupplementaryElementOfKind IS dequeueing a cell
  • The cell IS the correct element
  • The element is returning
  • viewForSupplementaryElementOfKind IS valid
  • viewForSupplementaryElementOfKind loads correctly when the collectionView first loads; this only occurs on refreshing

PLEASE, re-read the very top statement again before replying to ensure you are not replying "you/viewForSupplementaryElementOfKind is returning nil, check you are not returning nil". I will happily run additional test.

It would help if you could post your code of a) how you register the supplementary view and b) how you dequeue and configure it. Also the exact exception message would be helpful.

With the information provided here, my best guess is that you are registering one element kind but then return another element kind. I.e. the element kind passed to you in viewForSupplementaryOfKind: is not the same element kind that is set on the cell when you return it / the identifier of what you are dequeuing is associated with a different element kind during registration of that class.

This is an issue as it completely breaks reusability, so starting in iOS 15 this case is detected and asserted.

iOS 15 ONLY crash at -collectionView:viewForSupplementaryElementOfKind:atIndexPath:
 
 
Q