Adding iOS UITableView HeaderView (not section header)

AMM picture AMM · Mar 26, 2011 · Viewed 227.1k times · Source

I want to add a table header (not section headers) like in the contacts app for example: enter image description here

exactly like that - a label beside an image above of the table.

I want the all view be scrollable so I can't place those outside of the table.

How can I do that?

Answer

peterjb picture peterjb · Mar 26, 2011

UITableView has a tableHeaderView property. Set that to whatever view you want up there.

Use a new UIView as a container, add a text label and an image view to that new UIView, then set tableHeaderView to the new view.

For example, in a UITableViewController:

-(void)viewDidLoad
{
     // ...
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     [headerView addSubview:imageView];
     UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
     [headerView addSubview:labelView];
     self.tableView.tableHeaderView = headerView;
     [imageView release];
     [labelView release];
     [headerView release];
     // ...
}