繁体   English   中英

设置UITableView Section Header的颜色

[英]Set color of UITableView Section Header

如何将我所有部分的节头设置为redColor,而不将它们设置为具有相同的标题字符串,并设置字体和字体颜色。

我试过这个,但它摆脱了我的节标题

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    if (section == 1)
        [headerView setBackgroundColor:[UIColor redColor]];
    else
        [headerView setBackgroundColor:[UIColor clearColor]];
    return headerView;
}

我真的很感激一些示例代码。

提前致谢

如果您只是想要更改标题视图的背景和字体颜色,可以使用更简单的方法

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

有一个非常好的例子/解释在这里

尝试自定义节标题视图。 您可以扩展此方法。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];

    //Put your common code here
    // lets say you need all background color to be white. do this here
     [headerView setBackgroundColor:[UIColor redColor]];

    //Check the section and do section specific contents
    if (section == 1)
        [headerView setText:@"Section 1"];
    else
        [headerView setBackgroundColor:[UIColor clearColor]];

  return headerView;

}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    header.textLabel.textColor = [UIColor redColor];
    CGRect headerFrame = header.frame;
    header.textLabel.frame = headerFrame;
    [header.contentView setBackgroundColor:[UIColor blueColor]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM