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

First thing you have to do is check the box "Section Header" in the collection view's attribute inspector. Then add a collection reusable view just like you added your cell to the collection view, write an identifier and make a class for it if you need to. Then implement the method:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

From there do exactly like you did with cellForItemAtIndexPath Its also important to specify if its a header or footer you are coding about:

if([kind isEqualToString:UICollectionElementKindSectionHeader])
{
    Header *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerTitle" forIndexPath:indexPath];
    //modify your header
    return header;
}

else
{

    EntrySelectionFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"entryFooter" forIndexPath:indexPath];
    //modify your footer
    return footer;
}

use indexpath.section to know what section this is in also note that Header and EntrySelectionFooter are custom subclasses of UICollectionReusableView that I made


I would like to share my insight of the solution provided by rob mayoff but I can't post comment so I am putting it here:

For every one of you that tried to keep reference of the supplementary views being used by a collection view but who run into issues of loosing track too early because of

collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:

being called too many times, try using an NSMapTable instead of a dictionary.

I use

@property (nonatomic, strong, readonly) NSMapTable *visibleCollectionReusableHeaderViews;

created like this:

_visibleCollectionReusableHeaderViews = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];

so that when you are keeping a reference to a supplementary view:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // ( ... )
    [_visibleCollectionReusableHeaderViews setObject:cell forKey:indexPath];

it keeps only a WEAK reference to it in the NSMapTable and it keeps it AS LONG AS the object is not deallocated!

You don't need anymore to remove the view from

collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:

as the NSMapTable will lose the entry as soon as the view is deallocated.


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.