UITableView dequeueReusableHeaderFooterViewWithIdentifier
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
Parameters
identifier
A string identifying the header or footer view to be reused. This parameter must not be nil.
Return Value of [UITableView dequeueReusableHeaderFooterViewWithIdentifier]
A UITableViewHeaderFooterView object with the associated identifier or nil if no such object exists in the reusable view queue.
Discussion of [UITableView dequeueReusableHeaderFooterViewWithIdentifier]
For performance reasons, a table view's delegate should generally reuse UITableViewHeaderFooterView objects when it is asked to provide them. A table view maintains a queue or list of UITableViewHeaderFooterView objects that the table view's delegate has marked for reuse. It marks a view for reuse by assigning it a reuse identifier when it creates it (that is, in the initWithReuseIdentifier: method of UITableViewHeaderFooterView).
You can use this method to access specific template header and footer views that you previously created. You can access a view's reuse identifier through its reuseIdentifier property.
UITableView dequeueReusableHeaderFooterViewWithIdentifier example.
If you wanted to have a reusable UITableViewHeaderFooterView without subclassing then you simply change your viewDidLoad to this:
// Register the class for a header view reuse.
[_buttomTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];
and then your delegate method to this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";
// Reuse the instance that was created in viewDidLoad, or make a new one if not enough.
UITableViewHeaderFooterView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
// Add any optional custom views of your own
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 30.0)];
[customView setBackgroundColor:[UIColor blueColor]];
[sectionHeaderView.contentView addSubview:customView];
sectionHeaderView.textLabel.text = @"Non subclassed header";
return sectionHeaderView;
}
I hope that was clear enough.
// Register the class for a header view reuse.
[_buttomTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];
and then your delegate method to this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";
// Reuse the instance that was created in viewDidLoad, or make a new one if not enough.
UITableViewHeaderFooterView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
// Add any optional custom views of your own
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 30.0)];
[customView setBackgroundColor:[UIColor blueColor]];
[sectionHeaderView.contentView addSubview:customView];
sectionHeaderView.textLabel.text = @"Non subclassed header";
return sectionHeaderView;
}
I hope that was clear enough.
Example of [UITableView dequeueReusableHeaderFooterViewWithIdentifier].
In the viewDidLoad method, I registered the view like this:
[self.tableView registerNib:[UINib nibWithNibName:@"Header1" bundle:nil] forHeaderFooterViewReuseIdentifier:@"header1"];
Then, in the delegate methods:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header1"];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
[self.tableView registerNib:[UINib nibWithNibName:@"Header1" bundle:nil] forHeaderFooterViewReuseIdentifier:@"header1"];
Then, in the delegate methods:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header1"];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}