2

I have added <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> to my collection view and set up my UICollectionView as below.

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerReuseIdentifier];

I have also added the necessary methods.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.view.frame.size.width, 44.0f);
}

- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"supplementaryViewForElementKind called");
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                               withReuseIdentifier:headerReuseIdentifier
                                                                                      forIndexPath:indexPath];
        header.backgroundColor = [UIColor redColor];
        return header;
    }

    return nil;
}

However, supplementaryViewForElementKind never gets called. Any ideas?

user4992124
  • 1,474
  • 1
  • 12
  • 27

1 Answers1

0

If you have added the header view in Interface Builder and already assigned the class, then you should remove the registerClass call in your code. It should show then. Put a break in your delegate method to see it being called.

Lee Probert
  • 7,847
  • 8
  • 35
  • 49