iosobjective-cuicollectionviewsectionheaderuicollectionreusableview

UICollectionReusableView Section Header crashes because of IBOutlet connection


I have a UICollectionViewController which delegates UICollectionViewDataSource and UICollectionViewDelegate. My collection view displays 2 sections with multiple rows of data and works fine.

I have created a Section Header (in IB Attributes Inspector -> Accessories) which then subclasses UICollectionReusableView with the SWOHighScoreHeader class:

@interface SWOHighScoreHeader : UICollectionReusableView
@property (strong, nonatomic) IBOutlet UILabel *hiScoreHead;
@end

I set this class (SWOHighScoreHeader) as the Custom Class for the UICollectionReusableView in IB.

In the UICollectionViewController I use the method:

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

SWOHighScoreHeader *highScoreHeaderView = nil;

if ([kind isEqual:UICollectionElementKindSectionHeader]) {
    highScoreHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                             withReuseIdentifier:@"highScoreTableHead"
                                                                    forIndexPath:indexPath];
}

return highScoreHeaderView;
}

The identifier highScoreTableHead is set as the UICollectionReusableView Collection Reusable View Identifier in IB.

At this stage, the section headers display correctly, albeit with the default label text.

My issue comes when I connect the IBOutlet UILabel hiScoreHead property with the outlet in IB. When I do this, the program crashes with:

Unknown class SWOHighScoreHeader in Interface Builder file.

** * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key submitButton.'

I've tried deleting the outlet connection and reconnecting but still nothing. Any ideas where I am going wrong?


Solution

  • I solved this by using Tags instead of IBOutlets ie

    UILabel *hiScoreHeader = (UILabel *)[highScoreHeaderView viewWithTag:101];
    hiScoreHeader.text = @"Header Text";
    

    I'm not sure why the IBOutlets don't work but at least I have a solution.