views:

6971

answers:

2

Hi, how can I change color of a section header in UITableView?

Thanks.

+18  A: 

Hopefully this method from the UITableViewDelegate protocol will get you started:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
  if (section == integerRepresentingYourSectionOfInterest)
     [headerView setBackgroundColor:[UIColor redColor]];
  else 
     [headerView setBackgroundColor:[UIColor clearColor]];
  return headerView;
}

Replace [UIColor redColor] with whichever UIColor you would like. You may also wish to adjust the dimensions of headerView.

Alex Reynolds
It can also help to adjust the section header size using self.tableView.sectionHeaderHeight. Otherwise, you may have trouble seeing the text you display for the section title.
Tony Lenzi
Works great!!!!
Anthony Glyadchenko
+9  A: 

Thanks Alex!! I've been looking for this answer everywhere!

How about the text color though? I just get a background color but no text, even if I change the alpha to 0.5.

Much appreciated

ADDENDUM: I figured it out. Need to add this to Alex's Code Example above. Maybe this will help others:

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
DoctorG