iOS - 表格视图 - 静态单元格(已分组) - 更改节标题文本颜色

use*_*037 17 uitableview ios

概观

我有一个带有表视图的iOS项目,其中包含以下规范:

  • 静态单元格(内容未动态填充)
  • 风格分组

  1. 如何更改静态表视图的节头的文本颜色?

Jon*_*lli 31

您需要创建自己的标题视图:

在tableview数据源/委托中实现

- (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;
}
Run Code Online (Sandbox Code Playgroud)


小智 14

也可以做到这一点:

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

.....