Without using a storyboard we could simply drag a UIView
onto the canvas, lay it out and then set it in the tableView:viewForHeaderInSection
or tableView:viewForFooterInSection
delegate methods.
How do we accomplish this with a StoryBoard where we cannot drag a UIView onto the canvas
Just use a prototype cell as your section header and / or footer.
tableView:viewForHeaderInSection:
method or the tableView:viewForFooterInSection:
method[tableView dequeueReusableCellWithIdentifier:]
to get the headertableView:heightForHeaderInSection:
method.-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (headerView == nil){
[NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"];
}
return headerView;
}
Edit: How to change the header title (commented question):
tableView:viewForHeaderInSection:
method get the label by calling: UILabel *label = (UILabel *)[headerView viewWithTag:123];
[label setText:@"New Title"];