UICollectionReusableView method not being called

Eden V. picture Eden V. · Aug 11, 2013 · Viewed 46.1k times · Source

I want my sections in the UICollectionView to have a header with an image.

I have followed these steps:

  • at the storyboard, assigned a header as an accessory for my UICollectionView
  • gave it an identifier
  • created a subclass of UICollectionReusableView for it
  • assigned the custom class to the class at the storyboard.
  • put an ImageView at the header accessory
  • made an outlet for the ImageView at the custom class in the .h file
  • Implemented the following at viewDidLoad:

 

[self.collectionView registerClass:[ScheduleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader)
    {
        ScheduleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

        headerView.headerImageView.image = [UIImage imageNamed:@"blah.png"];

        reusableview = headerView;
    }

    return reusableview;
}

I know that the datasource and delegate methods are working because I can see all the cells and its sections. However, I don't have my headers. I put in a breakpoint at the method above and it's never being called.

What am i doing wrong?

Answer

rdelmar picture rdelmar · Aug 11, 2013

It seems that you have to give your header a non-zero size or collectionView:viewForSupplementaryElementOfKind:atIndexPath isn't called. So either set the headerReferenceSize property of the flow layout like so:

flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);

Swift 5+

flowLayout.headerReferenceSize = CGSize(CGSize(width: self.collectionView.frame.size.width, height: 100))

or, implement collectionView:layout:referenceSizeForHeaderInSection if you want to vary the size by section.