UITableView Cookbook

UITableView cell’s separator

tableView.separatorColor =  [UIColor colorWithRed:173.0/256.0 green:173.0/256.0 blue:176.0/256.0 alpha:1.0];

Customize UITableViewCell

  • backgroundView: custom background for all rows
  • contentView: content view of the cell
  • accessoryView: view on the right side of the cell

 

Add a UILabel to Header/Footer View

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 5, 320.0, 40.0);
    ;
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = @"your text here";
    label.numberOfLines = 0;
    ;
    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40.0)];
    [view autorelease];
    [view addSubview:label];
    return view;
 }

Hide UITableView header and scroll to first cell

In viewWillAppear controller, use setContentOffset method:

-(void)viewWillAppear:(BOOL)animated {
    [tableView setContentOffset:CGPointMake(0,tableView.tableHeaderView.frame.size.height)];
    [super viewWillAppear:animated];
}