I am attempting to create customized section headers for a UITableView. I have found a few references illustrating how to do this completely in code (#1349571).
I am trying to determine if it is possible to create a UIView in Interface Builder and use that to customize the header similar to how it is possible to do this for a UITableViewCell?
YES It is possible to use header view created using XIB. Create a XIB and the class to manage the XIB (UIView class). Use
YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject];
//With this method you can load any xib for header view
tableView.tableHeaderView = v;
[v release];
EDIT
Return this view in the viewForHeaderInSection like this
YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject];
//Do some stuff here like setting text on labels etc.
return [v autorelease];
Since we have prototype cells, you can also add a prototype cell to your table. Fill in the Identifier of the prototype cell in Interface Builder. (for example HeaderCell) You can then use it in viewForHeaderForSection like you use cells in cellForRowAtIndexPath.
Example:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [tableView dequeueReusableCellWithIdentifier:HeaderCellID];
UILabel *label = (UILabel *)[headerView viewWithTag:100];
label.text = [self tableView:self.tableView titleForHeaderInSection:section];
return headerView;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With