I have a collectionView with a custom layout, so far I only have one UICollectionReusableView
. At first everything works fine:
prepareLayout()
is called that fills up the cache,collectionViewContentSize()
is called and returns the right cententSize andlayoutAttributesForElementsInRect()
is called and return the right attributes.
This causes the collectionView's viewForSupplementaryElementOfKind
-method to be called, after which the view becomes visible.
Later, when I press a button, the size of the reuseable view changes, and collectionView.reloadData()
is called. This causes the 3 above mentioned methods to be called again, and they still returns the right values with new sizes, but this time it doesn't trigger viewForSupplementaryElementOfKind
, and the collectionView gets empty.
Why isn't viewForSupplementaryElementOfKind
called?
Debug example:
The return of layoutAttributesForElementsInRect
the first time the collection view is layed out (i.e. when it works)
layout attributes: [<UICollectionViewLayoutAttributes: 0x7fa5005dc970> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 414 1106); ]
The return of layoutAttributesForElementsInRect
the second time the collection view is layed out (i.e. when it doens't works)
layout attributes: [<UICollectionViewLayoutAttributes: 0x7fa500708f00> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 414 1560.67); ]
EDIT: Debugging:
I just tested using collectionView.reloadData()
without changing the size of the view. This caused the same problem, the collectionview is cleared. So it is not the fact that I'm changing in the attributes that is causing the problem.
EDIT 2: More debugging:
I tried using collectionView.collectionViewLayout.invalidateLayout()
instead og reloadData()
. This time I could see that the the new size of the cell changes properly. But when i call reloadData()
afterwards, I get the same problem. is makes me more confident that it is reloadData()
that causes the problem.