8

I have a collectionview and I want to use a xib as the header. However, the xib will not appear. First I tried adding a label to the xib, which didn't appear. Then I set the whole background color to red. It it doesn't appear. The collectionview items leave a gap for the header, but it's completely blank. Similar SO threads are this and this, but as I'm not using storyboards and my header height is greater than zero, they don't solve my issue. Here is all my relevant code:

#import "ScoreboardCollectionViewController.h"
#import "ScoreboardCollectionViewCell.h"
#import "ScoreboardReusableView.h"
#import "ScoreboardModel.h"
#import "Scoreboard.h"

...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register cell classes
    UINib *cellNib = [UINib nibWithNibName:@"ScoreboardCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];

    [self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];


    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_fish_skin_"]]; 
}

...

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

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

    ScoreboardReusableView *view = [[ScoreboardReusableView alloc] init];

    view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                              withReuseIdentifier:@"scoreboardHeader"
                                                     forIndexPath:indexPath];

    return view;
}

Here is a screenshot of my xib: enter image description here

Community
  • 1
  • 1
Jameson
  • 4,198
  • 4
  • 17
  • 31

2 Answers2

19

OK, I solved it. The problem was that in my viewDidLoad method I had

[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];

when since I was using a nib, it should have been:

[self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
          forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                 withReuseIdentifier:@"scoreboardHeader"];
Jameson
  • 4,198
  • 4
  • 17
  • 31
  • 1
    Thank you! I had the same issue, this helped me resolve my issue. I hadn't realized that I also had a typo which caused me a lot of heartache even after I discovered this post of yours. But that is my bad. – Curious101 Jul 20 '16 at 07:23
0

I was missing "Section Header" option of Collection View to enable:-

enter image description here

Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44