29

For performance sake it is usual to reuse UITableView' cells. Is there a way to do the same thing with TableView header' views? I am talking about the ones that are returned with delegate's method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

I tried to do the following which doesn't seem to be working as expected:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *CellIdentifier = @"Header";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [self getHeaderContentView: CellIdentifier];
    }
    return cell;
}

Is there a way to reuse header' views?

Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168

3 Answers3

35

The reason Apple built in the ability to reuse tableview cells is because while the tableview may have many rows, only a handful are displayed on screen. Instead of allocating memory for each cell, applications can reuse already existing cells and reconfigure them as necessary.

First off, header views are just UIViews, and while UITableViewCell is a subclass of UIView, they are not intended to be placed as the view of a section header.

Further, since you generally will have far fewer section headers than total rows, there's little reason to build a reusability mechanism and in fact Apple has not implemented one for generic UIViews.

Note that if you are just setting a label to the header, you can use -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section instead.

For something more custom, such as a label with red text (or a button, image, etc), you can do something like this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease];
  UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease];
  label.textColor = [UIColor redColor];
  label.text = [NSString stringWithFormat:@"Section %i", section];

  [headerView addSubview:label];
  return headerView;
}
Martin Gordon
  • 36,329
  • 7
  • 58
  • 54
  • I think that label shouldn't be autorelease but release after it has been added to headerView. What's your mind? – gsempe Jul 15 '11 at 08:17
  • 16
    "there's little reason to build a reusability mechanism..." Martins comment is generally correct, but not completely. For anyone who is trying to reach 60fps (like me), viewForHeaderInSection, with a custom view, is the devil. Initing a new view for each header, particularly if your groups are not large, will virtually make it impossible to reach said performance, as it will typically cost me 5 FPS. If you can avoid custom headers in your design, it will give you a performance boost. – Jason Cragun Aug 23 '11 at 15:15
  • 2
    But what if you really need to use custom header views, what's the best way to keep the FPS to 60? – Van Du Tran Feb 13 '13 at 16:20
  • For speed you can keep an array of all SectionViews. Thus creating every header only one time. – MacMark Sep 27 '13 at 09:14
  • 3
    Even though your explanation is thorough, this really doesn't answer the question. – olivaresF Jan 21 '14 at 02:31
  • 3
    Since iOS 6 we are supposed to reuse these as well: `UITableViewHeaderFooterView` – Julian F. Weinert Jan 05 '16 at 15:41
13

You can implement that by creating UITableViewHeaderFooterView class it is subclass of UIView You also need to create an individual XIB as it will not be created automatically with UITableViewHeaderFooterView creation.

Register Nib with tableview

[self.tblCart registerNib:[UINib nibWithNibName:@"CartHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CartHeader"];

Now You can Access that in viewForHeaderInSection

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    CartHeaderView *sectionHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CartHeader"];
 return sectionHeader;
}

Note : To set background color you will need to create a subview with same frame as section header and set color for that view.

you can follow

Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead

Community
  • 1
  • 1
Bhavik
  • 209
  • 2
  • 4
10

A simple yet effective solution:

@interface SectionedTableViewController ()
    @property (nonatomic, strong) UINib*          sectionHeaderNib;
    @property (nonatomic, strong) NSMutableArray* sectionHeaders;
@end

@implementation SectionedTableViewController

@synthesize sectionHeaderNib = sectionHeaderNib_;
@synthesize sectionHeaders = sectionHeaders_;

- (void) viewDidUnload
{
    self.sectionHeaders = nil;
    [super viewDidUnload];
}

- (NSMutableArray*) sectionHeaders
{
    if (!sectionHeaders_)
        self.sectionHeaders = [NSMutableArray array];
    return sectionHeaders_;
}


- (UINib*) sectionHeaderNib
{
    if (!sectionHeaderNib_)
        self.sectionHeaderNib = [UINib nibWithNibName: NSStringFromClass(YourHeaderView.class) bundle: nil];
    return sectionHeaderNib_;
}


- (YourHeaderView*) dequeueHeader
{
    return [self.sectionHeaders firstObjectPassingTest: ^(id obj) { return (BOOL) ([obj superview] == nil); }];
}


- (NSString*) sectionHeaderTitleForSection: (NSInteger) section
{
    return nil;
}


- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section
{
    YourHeaderView* headerView = [self dequeueHeader];
    if (!headerView)
    {
        headerView = [YourHeaderView instanceFromNib: self.sectionHeaderNib];
        [self.sectionHeaders addObject: headerView];
    }
    return headerView;
}

@end
Kostia Dombrovsky
  • 1,647
  • 2
  • 18
  • 29