user1046037
user1046037

Reputation: 17695

iOS - table view - static cells (grouped) - change section header text color

Overview

I have an iOS project with a table view with the following specification:

Question

  1. How can I change the text color of the section header of the static table view ?

Upvotes: 17

Views: 13998

Answers (4)

Brian M
Brian M

Reputation: 3932

In Swift 4.2:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.textLabel?.textColor = UIColor.OMGColors.dimText
    }
}

No need to make your own header view - that defeats the purpose of static cells. I'm surprised you can't set this directly in IB somewhere though...

Upvotes: 0

user2488988
user2488988

Reputation: 141

Can make this too:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    [[((UITableViewHeaderFooterView*) view) textLabel] setTextColor:[UIColor whiteColor]];
}

.....

Upvotes: 14

mSabu
mSabu

Reputation: 31

I was able to view the header only after adding height for header along with the view

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 110;
}

Upvotes: 1

Jonas Schnelli
Jonas Schnelli

Reputation: 10005

You need to create your own header view:

implement within your tableview datasource/delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(20, 6, 300, 30);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithHue:(136.0/360.0)  // Slightly bluish green
                                 saturation:1.0
                                 brightness:0.60
                                      alpha:1.0];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    // Create header view and add label as a subview

    // you could also just return the label (instead of making a new view and adding the label as subview. With the view you have more flexibility to make a background color or different paddings
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)];
    [view autorelease];
    [view addSubview:label];

    return view;
}

Upvotes: 31

Related Questions