Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There is a method to get a cell by indexPath (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?

like image 492
Dorian Roy Avatar asked Nov 15 '12 09:11

Dorian Roy


People also ask

How do I add a section header in collection view?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

What is supplementary view?

Supplementary views are more related to your data. The collection view layout still decides where to put them, but they are provided by the collection view data source, just like regular cells.


1 Answers

UPDATE

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

ORIGINAL

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.

like image 117
rob mayoff Avatar answered Oct 10 '22 22:10

rob mayoff