I have some data that is fetched in another thread that updates a UICollectionView's header. However, I've not found an efficient way of reloading a supplementary view such as a header or footer.
I can call collectionView reloadSections:
, but this reloads the entire section which is unnecessary. collectionView reloadItemsAtIndexPaths:
only seems to target cells (not supplementary views). And calling setNeedsDisplay
on the header itself doesn't appear to work either. Am I missing something?
I've used above method to get current header, and successfully updated subviews on it.
Swift 3/4/5 version:
Caution!
If you change the number of
collectionView
items at the same time (for example you show the footer only if all cells were loaded), it will crash. You need to reload the data first, to make sure that the number of items is the same before and afterinvalidateLayout()
:I got the same problem. I tried @BobVorks's answer and it is working fine, if only the cell was reused else it won't. So, I tried finding a more cleaner way to achieve this and I came up reloading the whole UICollectionView after the performBatchUpdate (completion block) and it is working great. It reloads the Collection Without any cancellation of animation in the insertItemsAtIndexPath. Actually I personally up voted recent 2 answers cause i find it working but in my case, this is the cleanest way to do it.