I am implementing custom section headers for a UITableView using the tableView:viewForHeaderInSection method. Everything is good except that a cell separator is still showing up above each section header. This does not look good. If you look closely at Apple's default section headers, they are actually one pixel taller than their set height (set in tableView:heightForHeaderInSection) and that extra pixel covers up the separator that would otherwise be displayed there. I am confident that there is something going on with Apple's default section header view creation that I am not mimicking in my own tableView:viewForHeaderInSection, but I cannot figure out what it is. If anyone has any insight or ideas, please let me know.
Code:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create the parent view
UIView * customSectionView = [[UIView alloc] initWithFrame:CGRectMake(0.0, -5, self.tableView.frame.size.width, [self tableView:tableView heightForHeaderInSection:section])];
customSectionView.backgroundColor = [[UIColor colorWithRed:0.306 green:0.161 blue:0.047 alpha:1.000] colorWithAlphaComponent:0.9];
// create the label
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, customSectionView.frame.size.height)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont fontWithName:@"Georgia" size:14];
headerLabel.text = @"Foo";
// package and return
[customSectionView addSubview:headerLabel];
[headerLabel release];
return [customSectionView autorelease];
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}