How to add Header with Supplementary view to UiCollectionView in Objective-C? - ios

I have a ViewController that contain a CollectionView :
- (void)viewDidLoad
{
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionview.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.86 alpha:1.0];
collectionview.translatesAutoresizingMaskIntoConstraints = false;
collectionview.delegate = self;
collectionview.dataSource = self;
collectionview.bounces = true;
[collectionview registerClass:[CollectionViewCell1 class] forCellWithReuseIdentifier:#"cell1"];
[self.view addSubview:collectionview];
[collectionview sdc_alignEdgesWithSuperview:UIRectEdgeAll];
[collectionview registerClass:[RecipeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
RecipeCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor greenColor];
headerView.title.text = #"ABC";
reusableview = headerView;
return reusableview;
}
and RecipeCollectionReusableView is :
- (void)initialize
{
_title = [UILabel new];
_title.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:_title];
[_title sdc_centerInSuperview];
}
but there isn't any header on the screen after run.

Did you do the required implementation for the header?
https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617702-collectionview?language=objc
collectionView:layout:referenceSizeForHeaderInSection:
Return Value
The size of the header. If you return a value of size (0, 0), no header is added.
Discussion
If you do not implement this method, the flow layout uses the value in its
headerReferenceSize
property to set the size of the header.
During layout, only the size that corresponds to the appropriate scrolling direction is used. For example, for the vertical scrolling direction, the layout object uses the height value returned by your method. (In that instance, the width of the header would be set to the width of the collection view.) If the size in the appropriate scrolling dimension is 0, no header is added.

This article completely helped me :
How to add Header wit Supplementary view to UiCollectionView in Objective-C

Related

Trouble with UICollectionView : does not display labels of all of its cells

I have created a UICollectionView programmatically. I use a custom UICollectionViewCell subclass. Inside the cell class, I create a label with a class of my own (easier and quicker to set up its appearance).
The problem I'm having is : for several cells, the collectionView does not layout the label content. I know that the data is here (print in the console) that is, the text property of the cell does contain the string data I want to show, but for some reason the collectionView does not display the label content.
I tried with a simple test (print 'toto' inside the label) and I get a few toto's here and there, but not in all of the cells. As you can see I have 2 UICollectionViews inside the same ViewController, and this is why I test whether it's one or the other in the DataSource implementation.
Please tell me if you need more code.
Here's the code :
-(void)createBottomCollectionView {
// Layout
UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionViewLayout.minimumLineSpacing = 0.0;
// UICollectionView
self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 354+20, 320-2*20, 35) collectionViewLayout:collectionViewLayout];
self.bottomCollectionView.showsHorizontalScrollIndicator = NO;
self.bottomCollectionView.bounces = YES;
self.bottomCollectionView.alwaysBounceHorizontal = YES;
self.bottomCollectionView.alwaysBounceVertical = NO;
self.bottomCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.bottomCollectionView.dataSource = self;
self.bottomCollectionView.delegate = self;
[self.bottomCollectionView registerClass:[SetFormatCollectionViewCell class] forCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier];
// Background
self.bottomCollectionView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.bottomCollectionView];
[self.bottomCollectionView reloadData];
}
CollectionView datasource (dumb test with a "toto" value) in the real app I pull data with CoreData
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.bottomCollectionView) {
SetFormatCollectionViewCell *cell = (SetFormatCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SetFormatCollectionViewCellIdentifier forIndexPath:indexPath];
cell.text = #"toto";
return cell;
}
if (collectionView == self.collectionView) {
TrackingSetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TrackingSetCollectionViewCellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
return nil;
}
Custom Cell class :
#interface SetFormatCollectionViewCell : UICollectionViewCell
#property (strong,nonatomic) NSString *text;
#end
#implementation SetFormatCollectionViewCell
{
FormatLabel *aFormatLabel;
}
-(id)initWithFrame:(CGRect)frame {
if (self=[super initWithFrame:frame]) {
// Initialization code
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.frame textColor:[UIColor blackColor] font:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];
[self.contentView addSubview:aFormatLabel];
}
return self;
}
-(void)prepareForReuse {
[super prepareForReuse];
self.text = #"";
}
-(void)setText:(NSString *)text {
_text = [text copy];
aFormatLabel.text = self.text;
}
FormatLabel Class (not important I think)
#interface FormatLabel ()
#property (assign,nonatomic) UIEdgeInsets edgeInsets;
#end
#implementation FormatLabel
-(id)initWithFrame:(CGRect)frame textColor:(UIColor *)color font:(UIFont *)font alpha:(CGFloat)alphaValue border:(BOOL)withBorder{
self = [super initWithFrame:frame];
if (self) {
// Set up
self.textAlignment = NSTextAlignmentCenter;
self.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
self.adjustsFontSizeToFitWidth = YES;
self.textColor = color;
self.alpha = alphaValue;
self.font = font;
if (withBorder) {
self.layer.borderWidth = 1.0f;
self.layer.borderColor = color.CGColor;
}
self.edgeInsets = UIEdgeInsetsMake(9, 6, 8, 6);
}
return self;
}
Thanks for helping
EDIT: for those who might have the same issue, I post 3 shots of the problem (you can find the answer just below). The 2nd shot contains colored cell the see the problem. The third shot is the one that I took just after accepting jmkk's answer.
Thanks for all the other answers!
Your problem lies with positioning of the FormatLabel view within the cell.
You're using cell's frame as the frame for the label, while what you need is the cell's bounds.
Cell's frame is relative to its superview, so applying the same position to cell's subview renders it offset relative to the cell itself.
Fix your code to do this:
aFormatLabel = [[FormatLabel alloc]initWithFrame:self.bounds textColor:[UIColor blackColor] font:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:22] alpha:1.0f border:YES];

Is it feasible to create custom CollectionViewLayout where spacing between individual cell can specifically control

I am trying to build a honeycomb using UiCollectionView . I have used https://github.com/cyrilchandelier/CCHexagonFlowLayout component for creating the basic structure of my view.
According to the requirement when any cell is tapped the adjacent cells should move out distance x from the tapped cell, with animation .
I have create two collectionViewLayout with different spacing value and changing it at didSelectItemAtIndexPath method
Here is my code …
#import "HoneycombViewController.h"
#interface HoneycombViewController ()
// Collection view
#property (nonatomic, strong) UICollectionView *collectionView;
#property(nonatomic,strong) CCHexagonFlowLayout *layout;
#property(nonatomic,strong) CCHexagonFlowLayout *largeLayout;
#property(nonatomic) CGFloat minimumItemSpacingFactor;
#property(nonatomic) CGFloat minimumLineSpacingFactor ;
#property(nonatomic) CGFloat layoutGapFactor ;
#end
#implementation A3HoneycombViewController
#synthesize minimumLineSpacingFactor,minimumItemSpacingFactor,layoutGapFactor;
- (void)viewDidLoad
{
[super viewDidLoad];
self.minimumItemSpacingFactor = 10.0f;
self.minimumLineSpacingFactor = 10.0f;
self.layoutGapFactor = 10.0f;
// Build layout
self.layout = [[CCHexagonFlowLayout alloc] init];
self.layout.delegate = self;
//layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.layout.minimumInteritemSpacing = -30.0f;
self.layout.minimumLineSpacing = 10.0f;
self.layout.sectionInset = UIEdgeInsetsMake(20.0f, 15.0f, 20.0f, 15.0f);
self.layout.itemSize = RootCell_SIZE;
//layout.headerReferenceSize = HeaderView_SIZE(layout.scrollDirection);
//layout.footerReferenceSize = FooterView_SIZE(layout.scrollDirection);
self.layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width,50) ;
self.layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width,50) ;
self.layout.gap = 76.0f;
//create large layout
self.largeLayout = [[CCHexagonFlowLayout alloc] init];
self.largeLayout.delegate = self;
self.largeLayout.scrollDirection = self.layout.scrollDirection;
self.largeLayout.minimumLineSpacing = self.layout.minimumLineSpacing+self.minimumLineSpacingFactor;
self.largeLayout.minimumInteritemSpacing = self.layout.minimumInteritemSpacing+self.minimumItemSpacingFactor;
self.largeLayout.itemSize = self.layout.itemSize;
self.largeLayout.sectionInset = self.layout.sectionInset;
self.largeLayout.headerReferenceSize =self.layout.headerReferenceSize;
self.largeLayout.footerReferenceSize = self.layout.footerReferenceSize;
self.largeLayout.gap = self.layout.gap + self.layoutGapFactor;
// Build collection view
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor];
// Register cell and views
[_collectionView registerNib:[RootCell cellNib] forCellWithReuseIdentifier:RootCell_ID];
[_collectionView registerNib:[HeaderView viewNib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderView_ID];
[_collectionView registerNib:[FooterView viewNib] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterView_ID];
// Add to view
[self.view addSubview:_collectionView];
// Reload data
[_collectionView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionDataSource methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return 10;
default:
return 10;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
RootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RootCell_ID forIndexPath:indexPath];
[cell configureWithInt:indexPath.item];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
// Section header
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
return [_collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:HeaderView_ID
forIndexPath:indexPath];
// Section footer
if ([kind isEqualToString:UICollectionElementKindSectionFooter])
return [_collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:FooterView_ID
forIndexPath:indexPath];
return nil;
}
-(CGSize)collectionViewContentSize { //Workaround
CGSize superSize = [self.layout collectionViewContentSize];
CGRect frame = self.collectionView.frame;
return CGSizeMake(fmaxf(superSize.width, CGRectGetWidth(frame)), fmaxf(superSize.height, CGRectGetHeight(frame)));
}
//
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayout *toLayout = self.layout == self.collectionView.collectionViewLayout ? self.largeLayout : self.layout;
[self.collectionView setCollectionViewLayout:toLayout animated:YES completion:^(BOOL finished) {
NSLog(#"animation end ");
}];
}
In the screenshot2 I have tapped in cell 1 now adjacent cells like 0 ,5,6,7,2 moved correctly as marked in green but other cells also moved marked in red .
As per this code all cell layout is changed but I want only my tabbed cell should be stick to its position and change its cell spacing show that it seems like its adjacent cells are move away at distance x also other cells in between space should remain with older value. Is it feasible to create custom CollectionViewLayout where spacing between individual cell can specifically control ? Also how we can use different CollectionViewLayout for different cells?

iOS UICollectionView header & footer location

Working in iOS 7, how does one specify where the header & footer boxes go in a UICollectionView?
I have a custom UICollectionViewFlowLayout. I have overwritten
-(void)prepareLayout
-(NSArray*) layoutAttributesForElementsInRect:(CGRect)rect
-(UICollectionViewLayoutAttributes*) layoutAttributesForSupplementaryViewOfKind: (NSString*)kind atIndexPath:(NSIndexPath*)indexPath
My problem is, I'm not sure how to specify header location. I have already specified that a header exists in prepareLayout:
-(void)prepareLayout
{
[super prepareLayout];
boundsSize = self.collectionView.bounds.size;
midX = boundsSize.width / 2.0f;
curIndex = 0;
self.headerReferenceSize = CGSizeMake(CELL_SIZE, TITLE_HEIGHT);
self.footerReferenceSize = CGSizeMake(0, 0);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET);
self.minimumLineSpacing = LINE_SPACING;
self.minimumInteritemSpacing = INTERIM_SPACING;
self.itemSize = CGSizeMake(CELL_SIZE, CELL_SIZE);
}
I just don't know the right property of my custom FlowLayout to set, as there doesn't seem to be something like "HeaderLocation" to set, either as a LayoutAttributes or in the layout object itself. Right now, it is appearing to the side/between my images, when I'd like them to be appearing above each image (horizontal scroll).
I have tried the following:
-(UICollectionReusableView*) collectionView: (UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{
NSLog(#"**ViewForSupplementaryElementOfKind called***");
CGFloat centerX = collectionView.center.x;
CGFloat centerY = collectionView.center.y;
CGFloat titleWidth = [MyLayout titleWidth];
CGFloat titleHeight = [MyLayout titleHeight];
MyTitleView* titleView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ImageTitleIdentifier forIndexPath:indexPath];
titleView.frame = CGRectMake(centerX - titleWidth/2.0,
0.0,
titleWidth,
titleHeight);
return titleView;
}
This doesn't work. The title appears above overlapped with a bunch of other titles, then the moment I start scrolling (horizontally), they jump back into the wrong place, horizontally between the images rather than above.
PS> Please do not suggest anything that has to do with NIB or XIB placement. I am using a UICollectionView, NOT a UICollectionViewController, so I actually have no prototypical cell to work with. The layout is being done entirely programatically -- from code alone -- so I can't simply open a XIB file and adjust the location of a text box.
Amending the attributes returned by -layoutAttributesForElementsInRect is the right approach, but if you want to alter the position of offscreen headers and footers, you may need to fetch the supplementary view attributes yourself.
For example, in your UICollectionViewFlowLayout subclass:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *attributesArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
// the call to super only returns attributes for headers that are in the bounds,
// so locate attributes for out of bounds headers and include them in the array
NSMutableIndexSet *omittedSections = [NSMutableIndexSet indexSet];
for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
if (attributes.representedElementCategory == UICollectionElementCategoryCell) {
[omittedSections addIndex:attributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
[omittedSections removeIndex:attributes.indexPath.section];
}
}
[omittedSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:indexPath];
[attributesArray addObject:attributes];
}];
for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
// adjust any aspect of each header's attributes here, including frame or zIndex
}
}
return attributesArray;
}
CollectionView Header height is set below Collectionview delegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
And Set view in Collectionview Header in Below Delegate
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView * view = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
ColorSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:NSStringFromClass([ColorSectionHeaderView class])
forIndexPath:indexPath];
header.sectionIndex = indexPath.section;
header.hideDelete = collectionView.numberOfSections == 1; // hide when only one section
header.delegate = self;
view = header;
}
return view;
}
Ragistred Class in ViewDidLoad
-(void)ViewDidLoad
{
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ColorSectionFooterView class]) bundle:nil]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:NSStringFromClass([ColorSectionFooterView class])];
[Super ViewDidLoad];
}

How to programmatically add header on UICollectionView with UICollectionViewLayout

I have a UICollectionView in one of my viewcontroller. My collection view uses a subclass of UICollectionViewLayout (custom) to layout the cells. First thing, as soon as I select Layout as Custom in dropdown on Storyboard, option to select supplementary views goes away.
I tried doing that programatically as shown below, but none of the delegate methods are getting called.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:#"Recipe Group #%li", indexPath.section + 1];
[reusableview addSubview:label];
return reusableview;
}
return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}
In my viewDidLoad Method I have
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView"];
Can anyone point me where I'm messing up?
You're passing in the incorrect view kind.
Your line registering the class:
[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:#"HeaderView"];
Should be:
[self.collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader
withReuseIdentifier:#"HeaderView"];
Edit: Looks like your code is all using sectionFooter. Are you trying to programmatically add a header or a footer?
Found the issue, I was not returning attributes of my header in this UICollectionLayoutView method:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect
Check that you gave reference size for header in UICollectionViewFlowLayout
[flowLayout setHeaderReferenceSize:CGSizeMake(320, 50)];
and for footer
[flowLayout setFooterReferenceSize:CGSizeMake(320, 50)];
Your delegate method for header reference size is wrong, you call footer method,referenceSizeForFooterInSection, as follow:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize headerSize = CGSizeMake(320, 44);
return headerSize;
}
Individually set HeaderReferenceSize will fix the header problem. But app will crash you keep the above method and return nil in viewForSupplementaryElementOfKind for Footer.
your check:
if (kind == UICollectionElementKindSectionFooter)
Your should check for: UICollectionElementKindSectionHeader
same for:
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
I think you should call this in your viewdidload method:
[collectionViewFlowLayout setHeaderReferenceSize:CGSizeMake(self.collectionView.frame.size.width, 50)];

UICollectionView header position in horizontal scroll direction mode with flow layout

I have ios UICollectionView with Flow layout with horizontal scroll direction.
In this situation typical header position is on the left side of cells.
I want to make header on the top of section cells
Have you any idea how can I do this?
I solved the problem using DateFlowLayout.
See how I configured it in this other answer.
I think you can get what you need using standard behavior for the header.
Set it up (probably in viewDidLoad):
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30);
// other setup
[self.collectionView setCollectionViewLayout:flowLayout];
Then answer a header view:
#define MY_HEADER_LABEL_TAG 128
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:#"SectionHeader" forIndexPath:indexPath];
UILabel *label = (UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG];
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
label.tag = MY_HEADER_LABEL_TAG;
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor darkGrayColor];
[headerView addSubview:label];
}
label.text = [NSString stringWithFormat:#"Section %d", indexPath.section];
return headerView;
}

Resources