Dorian Roy Dorian Roy - 7 months ago 96
iOS Question

UICollectionView: How to get the header view for a section?

There is a method to get a cell by

UICollectionView cellForItemAtIndexPath:
). But I can"t find a method to get one of the supplementary views like a header or footer, after it has been created. Any ideas?



As of iOS 9, you can use -[UICollectionView supplementaryViewForElementKind:atIndexPath:] to get a supplementary view by index path.


Your best bet is to make your own dictionary mapping index paths to supplementary views. In your collectionView:viewForSupplementaryElementOfKind:atIndexPath: method, put the view into the dictionary before returning it. In your collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:, remove the view from the dictionary.