Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize tableView Section Header with a XIB?

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?

like image 947
markdorison Avatar asked Jun 14 '11 01:06

markdorison


2 Answers

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];
like image 70
Rahul Vyas Avatar answered Sep 20 '22 07:09

Rahul Vyas


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;
}
like image 30
Bocaxica Avatar answered Sep 23 '22 07:09

Bocaxica