Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Header xib not showing

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

like image 836
Jameson Avatar asked Jun 01 '15 21:06

Jameson


1 Answers

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"];
like image 120
Jameson Avatar answered Nov 06 '22 10:11

Jameson