0

The viewForHeaderInSection's textLabel's textColor is always gray. BackgroundColor changes as expected. The following is the code creating the headerview. It does get in to the "!headerView" condition. Using ios sdk 7.1

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerIdentifier = @"InspectionQuestionHeader";

    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerIdentifier];

    if (!headerView) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerIdentifier];
        headerView.contentView.backgroundColor = [UIColor grayColor];

        // Why doesn't this work?
        headerView.textLabel.textColor = [UIColor whiteColor];
    }

    InspectionQuestionSection *questionSection = _questionSections[section];
    NSString *title = [NSString stringWithFormat:@"%@. %@", questionSection.sectionNumber, questionSection.sectionDescription];
    headerView.textLabel.text = title;

    return headerView;
}
Austin Haws
  • 364
  • 2
  • 6
  • 15
  • Are you setting a default appearance color for `UILabel` somewhere, or are you changing the color of the header's text anywhere in your controller? Because your code works fine for me (I only replaced the text on the header) – Stonz2 Jun 11 '14 at 14:09
  • On [this page](http://codehappily.wordpress.com/2013/10/07/ios-how-to-customize-table-view-header-and-footer-colors/) the color is changed in tableView:willDisplayHeaderView:forSection: – n-o-d Jun 11 '14 at 14:11
  • Ah- you must be using a grouped style table view. – Stonz2 Jun 11 '14 at 14:16
  • 1
    possible duplicate of [How to change font color of the title in grouped type UITableView?](http://stackoverflow.com/questions/7105747/how-to-change-font-color-of-the-title-in-grouped-type-uitableview) – Stonz2 Jun 11 '14 at 14:16
  • no default appearances; controller doesn't do anything with headers or labels, though I also suspect something along those lines. I tried adding a uilabel to the headerView.contentView and I can get that to be white. I'll dig some more to make sure there's no defaults or nefarious activity. – Austin Haws Jun 11 '14 at 14:16
  • i went to that duplicate link and it mentioned the apple docs said it could also be a uilabel instead of just UITableViewHeaderFooterView. Tried returning just a uilabel and it works great! YAY! – Austin Haws Jun 11 '14 at 14:27

1 Answers1

1

This is something you want to put in a different delegate method. Try this:

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
[headerIndexText.textLabel setTextColor:[UIColor whiteColor]];
}
StevenOjo
  • 2,498
  • 1
  • 16
  • 21