SundayMonday
SundayMonday

Reputation: 19767

Background color of UITableViewCell's header

I'd like to make the backgroundColor of a UITableView's section header view transparent. I don't want to format the text in the header because I like the default formatting. Can I do this with:

-(UIView*) tableView:(UITableView*)tableView 
            viewForHeaderInSection:(NSInteger)section

without having to format the text in a UILabel? Everything I've tried covers up the text in the section header (that I get from tableView:titleForHeaderInSection:) and I don't know how to format the text myself.

Upvotes: 0

Views: 2055

Answers (2)

Robert
Robert

Reputation: 361

You can use the existing default headers UITableViewHeaderFooterView and change values of it. This way you don't have to create the TextLabel yourself and can still use tableView:titleForHeaderInSection:

Be Sure to register the reuseIdentifyer first with:

[self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];

Example:

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    header.contentView.backgroundColor = [UIColor redColor];
    header.textLabel.textColor = [UIColor whiteColor];
    return header;
}

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

Well, table cells do not have a header.

The UITableView's section header is an independent view.

And yes,

-(UIView*) tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section

is THE table delegate method to implement. It returns a UIView (derivate) that displays the table header.

Make sure that you implement

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

as well and return the appropriate height for each header. (Probably just a constant value)

Upvotes: 2

Related Questions