UITableView

醉酒当歌 提交于 2020-03-18 18:04:23

3 月,跳不动了?>>>

如何在UITableView中更改节标题的颜色?

编辑DJ-S提供答案应考虑适用于iOS 6及更高版本。 接受的答案已过时。


#1楼

不推荐在UITableViewHeaderFooterView上设置背景颜色。 请改用contentView.backgroundColor


#2楼

如果您想要具有自定义颜色的标题,可以执行此操作:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

这个解决方案从iOS 6.0开始运行良好。


#3楼

如果您不想创建自定义视图,还可以更改颜色(需要iOS 6):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}

#4楼

这是一个老问题,但我认为答案需要更新。

此方法不涉及定义和创建自己的自定义视图。 在iOS 6及更高版本中,您可以通过定义来轻松更改背景颜色和文本颜色

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

部分委托方法

例如:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

取自我的帖子: https//happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

斯威夫特3/4

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}

#5楼

以下是更改文本颜色的方法。

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!