Just use a prototype cell as your section header and / or footer.
- add an extra cell and put your desired elements in it.
- set the identifier to something specific (in my case SectionHeader)
- implement the
tableView:viewForHeaderInSection:
method or the tableView:viewForFooterInSection:
method
- use
[tableView dequeueReusableCellWithIdentifier:]
to get the header
- implement the
tableView: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):
- Add a label to the header cell
- set the tag of the label to a specific number (e.g. 123)
- In your
tableView:viewForHeaderInSection:
method get the label by calling:
UILabel *label = (UILabel *)[headerView viewWithTag:123];
- Now you can use the label to set a new title:
[label setText:@"New Title"];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…