ios UITableView - 更改部分标题颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/813068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:52:25  来源:igfitidea点击:

UITableView - change section header color

iosuitableviewcocoa-touch

提问by Ilya Suzdalnitski

How can I change color of a section header in UITableView?

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

EDIT: The answer provided by DJ-Sshould be considered for iOS 6 and above. The accepted answer is out of date.

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

回答by Dj S

This is an old question, but I think the answer needs to be updated.

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

This method does not involve defining and creating your own custom view. In iOS 6 and up, you can easily change the background color and the text color by defining the

此方法不涉及定义和创建您自己的自定义视图。在 iOS 6 及更高版本中,您可以通过定义

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

section delegate method

节委托方法

For example:

例如:

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

Taken from my post here: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

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

Swift 3 / 4

斯威夫特 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
}

回答by Alex Reynolds

Hopefully this method from the UITableViewDelegateprotocol will get you started:

希望UITableViewDelegate协议中的这种方法可以帮助您入门:

Objective-C:

目标-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
}

Updated 2017:

2017 年更新:

Swift 3:

斯威夫特 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
    }

Replace [UIColor redColor]with whichever UIColoryou would like. You may also wish to adjust the dimensions of headerView.

替换[UIColor redColor]UIColor您喜欢的任何一个。您可能还希望调整 的尺寸headerView

回答by DoctorG

Here's how to change the text color.

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

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

回答by Leszek Zarna

You can do this if you want header with custom color:

如果您想要带有自定义颜色的标题,您可以这样做:

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

This solution works great since iOS 6.0.

自 iOS 6.0 以来,此解决方案效果很好。

回答by Max

The following solution works for Swift 1.2 with iOS 8+

以下解决方案适用于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()

}

回答by Alex

Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColorinstead.

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

回答by Steve

You can do it on main.storyboard in about 2 seconds.

您可以在大约 2 秒内在 main.storyboard 上完成。

  1. Select Table View
  2. Go to Attributes Inspector
  3. List item
  4. Scroll down to View subheading
  5. Change "background"
  1. 选择表格视图
  2. 转到属性检查器
  3. 项目清单
  4. 向下滚动到查看副标题
  5. 换背景”

Have a look here

看看这里

回答by whyoz

Don't forget to add this piece of code from the delegate or your view will be cut off or appear behind the table in some cases, relative to the height of your view/label.

不要忘记从委托中添加这段代码,否则在某些情况下,相对于视图/标签的高度,您的视图将被切断或出现在表格后面。

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

回答by William Jockusch

If you don't want to create a custom view, you can also change the color like this (requires iOS 6):

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

回答by Roozbeh Zabihollahi

Set the background and text color of section area: (Thanks to William Jockuschand Dj S)

设置分区的背景和文字颜色:(感谢William JockuschDj S

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        castView.contentView.backgroundColor = [UIColor grayColor];
        [castView.textLabel setTextColor:[UIColor grayColor]];
    }
}