13

I have a collectionView with a custom layout, so far I only have one UICollectionReusableView. At first everything works fine:

  1. prepareLayout() is called that fills up the cache,
  2. collectionViewContentSize() is called and returns the right cententSize and
  3. layoutAttributesForElementsInRect() 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.

Wiingaard
  • 4,150
  • 4
  • 35
  • 67
  • did you get any more insight on this? – Micky Mar 07 '17 at 15:57
  • I'm also curious if there is some progress on this one? – Baki Mar 29 '17 at 12:33
  • Sorry, I haven't looked into this since about the time i wrote the question.. – Wiingaard Mar 29 '17 at 12:35
  • No problems, I actually managed to solve issue few hours later. For info, if someone needs it, I had problem with layout calculation where I had wrongly calculated positions for cells in custom flow layout. – Baki Apr 01 '17 at 21:22
  • Similar sounding problem with (50%) overlapping cells. `reloadData()` on the collectionView seems to cause the cells to change position in the heirarchy. IOW, where cell 2 was on top of cell 1, after call, it is always under 1. – David Apr 13 '17 at 13:39

0 Answers0