I have a UICollectionView
. I would like to add a header. My header would only be a UILabel
. I\'ve :
1) selected \"Section Header\" as a Col
If you don't set the header view in storyboard, you will have to register nib.
Example in viewDidLoad:
- (void)viewDidLoad
{
[self.collectionView registerClass:NSStringFromClass([YourOwnSubClass class]) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewIdentifier"];
}
Anyway, you can also subclass UICollectionReusableView.
@interface YourOwnSubClass : UICollectionReusableView
then call delegate in your class example:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
YourOwnSubClass *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
[self updateSectionHeader:headerView forIndexPath:indexPath];
return headerView;
}
- (void)updateSectionHeader:(UICollectionReusableView *)header forIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [NSString stringWithFormat:@"header #%i", indexPath.row];
header.label.text = text;
}
And don't forget to set header size in collection view or in flow layout so header view will be visible.