May 25

Here is an easy way to create headers and footers for iPhone Table Views.

  1. In your Xcode project right-click on the Resources folder
  2. Select: Add / New File … / iPhone OS / User Interfaces / View XIB / [Next]
  3. Call it HeaderView.xib and click Finish
  4. GIve the View a unique background color and set the height to 44
  5. Repeat for FooterView.xib
  6. Save both files

In your UITableViewController class modify viewDidLoad to contain the following (the editing = YES line is optional – just showing you how to display those cool delete widgets):

- (void)viewDidLoad {
 
	self.tableView.editing = YES;	// Enable delete buttons.
 
	UIView *hView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] objectAtIndex:0];
 
	self.tableView.tableHeaderView = hView;
 
	UIView *fView = [[[NSBundle mainBundle] loadNibNamed:@"FooterView" owner:self options:nil] objectAtIndex:0];
 
	self.tableView.tableFooterView = fView;
}

As far as I can tell you can’t lock the header and footer in place. That will require a custom table view.

Tags: , , , ,

Leave a Reply