UITableView - 更改分区标题颜色

352

如何更改UITableView中分区标题的颜色?

编辑:iOS 6及以上版本应考虑使用DJ-S提供的答案,已接受的答案已过时。


4
我非常感激有关更新的iOS版本的编辑。 - Suz
34个回答

778

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

这种方法不涉及定义和创建自定义视图。 在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/

Swift 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
}

4
我不知道这已经被添加到SDK中了。太棒了!绝对是正确的答案。 - Jarrod Robins
1
OP - 请更新已接受的答案为此答案。比旧方法更简洁。 - Kyle Clegg
10
这对我似乎没有起作用。文字颜色可以正常显示,但头部背景的色彩却无法呈现。我的设备运行的是iOS 7.0.4。 - zeeple
11
您可以使用header.backgroundView.backgroundColor=[UIColor blackColor];来设置表头背景的色调。 - 慭慭流觞
2
@Kent 显然已经有一段时间了,但对于未来的人们,header.contentView.backgroundColor = [UIColor blackColor];选项将为您提供不透明的标题。 - SparkyRobinson
显示剩余4条评论

399

希望下面这个方法来自UITableViewDelegate协议能够帮助你开始:

Objective-C:

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

Swift:

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
  let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
  if (section == integerRepresentingYourSectionOfInterest) {
    headerView.backgroundColor = UIColor.redColor()
  } else {
    headerView.backgroundColor = UIColor.clearColor()
  }
  return headerView
}

2017年更新:

Swift 3:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
        if (section == integerRepresentingYourSectionOfInterest) {
            headerView.backgroundColor = UIColor.red
        } else {
            headerView.backgroundColor = UIColor.clear
        }
        return headerView
    }

用你希望的UIColor替换[UIColor redColor]。你可能还想调整headerView的尺寸。


17
使用self.tableView.sectionHeaderHeight可以调整节标题的大小,这样做可以避免在显示节标题文本时出现问题。 - Tony Lenzi
12
请注意,这个答案(虽然正确)将只返回一个没有内容的 UIView。 - Greg M. Krsak
除非提问者明确定义他们试图创建的视图类型,否则任何答案最多只能展示可以做什么的一般想法。希望这对你有所帮助,Greg。 - Alex Reynolds
我甚至可以设置大小为0,它仍然可以工作:CGRectMake(0,0,0,0) - catamphetamine
7
这些信息相当过时,仅仅创建另一个视图并不是最好的答案。关键是获得适当的视图并在其上更改颜色或色调。下面使用willDisplayHeaderView的方法是更好的做法。 - Alex Zavatone
显示剩余5条评论

99

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

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];

18
谢谢DoctorG——这很有用。顺便说一下,为了保留数据源提供的现有标签,我修改了第二行代码,如下所示:label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section]; 这可能不是最好的方法,但对我很有效。也许这可以帮助其他人。 - JJ Rohrer
1
@JJ 那个表单实际上很好,因为你调用的是最初用于定义表格部分标题的相同方法。 - Tim
3
我将移除自动释放并改为显式释放。UITableView的格式化方法会被频繁调用很多次,尽可能避免使用自动释放。 - memmons
1
在viewForHeaderInSection中,你应该直接返回UILabel而不是使用addSubview:UILabel。因为UILabel已经是一个UIView了 :) - Nas Banov
请注意,UILabel的默认文本样式可能与UITableView部分的默认文本样式不同。您可能需要调整标签以匹配适当的样式。 - Greg M. Krsak
显示剩余2条评论

58

如果您想要具有自定义颜色的标题,可以这样做。这个解决方案在iOS 6.0以后非常有效。

Objective C:

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

Swift:

UITableViewHeaderFooterView.appearance().tintColor = .white

1
嗯...对我来说不起作用。我尝试了iOS 6模拟器和iOS 7设备。你测试过这种方式吗?我应该把它放在哪里? - Maxim Kholyavkin
可以在应用程序委托的application:didFinishLaunchingWithOptions:方法中完成。 - Leszek Zarna
我的错:我尝试使用UITableViewStyleGrouped这种方式。顺便说一下,应该使用这种方式来改变文本颜色https://dev59.com/Q3A75IYBdhLWcg3wdIl0#20778406 - Maxim Kholyavkin
如果是自定义的UIView,只需将其放在-init方法中即可。 - felixwcf
其他解决方案对我没有用,但这个解决方案有效。 - Free Bird

34

以下解决方案适用于使用iOS 8+的Swift 1.2

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This changes the header background
    view.tintColor = UIColor.blueColor()

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour
    var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel.textColor = UIColor.redColor()

}

25

设置UITableViewHeaderFooterView的背景颜色已被弃用,请改用contentView.backgroundColor


22

你可以在main.storyboard上大约 2 秒钟内完成此操作。

  1. 选择表视图
  2. 进入属性检查器
  3. 列表项
  4. 向下滚动到“View”小标题
  5. 更改“background”

看这里


20

别忘了从代理中添加这段代码,否则你的视图可能会在某些情况下被裁剪或出现在表格后面,相对于你的视图/标签的高度。

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

如果您遵循Dj S的iOS6及更高版本答案,则不再需要此内容。 - Bjinse

18

如果您不想创建自定义视图,也可以像这样更改颜色(需要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;
    }
}

14

针对 Swift 5 +

willDisplayHeaderView 方法中

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

     //For Header Background Color
     view.tintColor = .black

    // For Header Text Color
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = .white
}
我希望这能对你有所帮助:]

1
让header = 视图 as! UITableViewHeaderFooterView - BhushanVU

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接