ChatGPT解决这个技术问题 Extra ChatGPT

UITableView - 更改部分标题颜色

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

编辑:对于 iOS 6 及更高版本,应考虑使用 answer provided by DJ-S。接受的答案已过时。

我真的很感谢编辑 RE 更新的 iOS 版本。

P
Peter Kreinz

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

此方法不涉及定义和创建您自己的自定义视图。在 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
}

我不知道这甚至已添加到 SDK 中。杰出的!绝对正确答案。
OP - 请更新已接受的答案。比旧方法清洁得多。
这似乎对我不起作用。文本颜色有效,但标题背景的色调无效。我在 iOS 7.0.4
user1639164,可以使用 header.backgroundView.backgroundColor=[UIColor blackColor];设置标题背景的色调。
@Kent 显然已经有一段时间了,但对于未来的人来说,header.contentView.backgroundColor = [UIColor blackColor]; 选项会给你一个不透明的标题
F
FluffyKitten

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

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

迅速:

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 年更新:

斯威夫特 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 redColor] 替换为您想要的任何 UIColor。您可能还希望调整 headerView 的尺寸。


它还可以帮助使用 self.tableView.sectionHeaderHeight 调整节标题大小。否则,您可能无法看到为部分标题显示的文本。
[UIColor xxxColor] 一起工作正常,但是当我尝试像我可以从 photoshop 获得的自定义颜色时(所以使用 UIColor red:green:blue:alpha:,它只是白色。我做错了什么吗?
发布一个单独的问题,我们会尽力提供帮助。包括源代码。
请注意,这个答案(虽然正确)只会返回一个没有内容的 UIView。
这是相当过时的信息,简单地创建另一个视图并不是最好的答案。我们的想法是获得正确的视图并更改其颜色或色调。下面使用 willDisplayHeaderView 的答案是一种更好的方法。
p
pkamb

这是更改文本颜色的方法。

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

谢谢 DoctorG - 这很有用。顺便说一句 - 为了保留数据源提供的现有标签,我修改了第二行,如下所示: label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];可能是不好的形式,但它对我有用。也许这可以帮助别人。
@JJ该表单实际上很好,因为您调用的方法与您最初用于定义表的节标题相同的方法。
我删除了自动释放并将其更改为显式释放。 UITableView 格式化方法被调用了很多很多次。尽可能避免使用自动释放。
@Harkonian,而不是更改提交的答案,请在对答案的评论中推荐更改。通过编辑更改其他人的代码被认为是一种不好的形式。拼写错误,错误的格式和语法是公平的游戏。
而不是 addSubview:UILabel,您应该只在 viewForHeaderInSection 中返回 UILabel。 UILable 已经是一个 UIView 了:)
D
Denis Kutlubaev

如果您想要具有自定义颜色的标题,您可以这样做。自 iOS 6.0 以来,此解决方案运行良好。

目标 C:

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

迅速:

UITableViewHeaderFooterView.appearance().tintColor = .white

嗯......它对我不起作用。尝试了 iOS 6 模拟器和 iOS 7 设备。你是这样测试的吗?我应该把它放在哪里?
它可以在 application:didFinishLaunchingWithOptions: 应用程序委托的方法中完成。
我的错:我尝试使用这种方式,而 UITableViewStyleGrouped 顺便说一句:应该使用这种方式更改文本颜色stackoverflow.com/a/20778406/751932
如果是自定义的UIView,只要放到-init方法中即可。
其他解决方案对我不起作用,但它起作用了。
M
Max

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

}

A
Alex

在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改用 contentView.backgroundColor


S
Steve

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

选择表视图转到属性检查器列表项向下滚动到视图子标题更改“背景”

https://i.stack.imgur.com/Q808i.png


w
whyoz

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

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

如果您遵循 Dj S 的 iOS6 和更高版本的回答,则不再需要这样做。
W
William Jockusch

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

t
theNoobDev10

对于迅捷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
}

我希望这可以帮助你 :]


让标题=查看为! UITableViewHeaderFooterView
R
Roozbeh Zabihollahi

设置部分区域的背景和文字颜色:(感谢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]];
    }
}

N
Nii Mantse

斯威夫特 4

要更改 UITableView 部分的标题视图的背景颜色文本标签颜色字体,只需为表格视图覆盖 willDisplayHeaderView像这样:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.backgroundView?.backgroundColor = .white
        header.textLabel?.textColor = .black
        header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
} 

这对我来说非常有效;希望它也能帮助你!


在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。您必须将具有所需背景颜色的自定义 UIView 设置为 backgroundView 属性。
a
animuson

以下是在标题视图中添加图像的方法:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];

    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

    [headerView addSubview:headerImage];

    return headerView;
}

C
Corona

对于 iOS8 (Beta) 和 Swift 选择你想要的 RGB 颜色并试试这个:

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()

    header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
    return header

}

(“覆盖”在那里,因为我在我的项目中使用 UITableViewController 而不是普通的 UIViewController,但更改部分标题颜色不是强制性的)

您的标题文本仍将可见。请注意,您将需要调整节标题高度。

祝你好运。


A
A.J. Hernandez

斯威夫特 2

我能够通过添加模糊效果成功更改部分背景颜色(这真的很酷)。要轻松更改部分的背景颜色:

首先转到情节提要并选择表格视图转到属性检查器列表项向下滚动以查看更改“背景”

然后为了模糊效果,添加代码:

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

    // This is the blur effect

    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
    let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel!.textColor = UIColor.darkGrayColor()
    headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
    headerView.tintColor = .groupTableViewBackgroundColor()
    headerView.backgroundView = blurEffectView

}

D
David Sanford

Swift 4 让它变得非常简单。只需将其添加到您的班级并根据需要设置颜色。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }

或者如果是简单的颜色

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor.white
    }

为 Swift 5 更新

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }

或者如果是简单的颜色

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.white
    }

在 iOS 13 中,将“view.backgroundColor”替换为“view.tintColor”。
按预期工作......(0_0)谢谢。!
a
arango_86

我知道它的答案,以防万一,在 Swift 中使用以下

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let tableViewWidth = self.tableView.bounds

        let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
        headerView.backgroundColor = UIColor.greenColor()

        return headerView
    }

S
SuryaKantSharma

对我来说,在浪费了 2 个小时之后,以上都不起作用,这是解决方案。在我的情况下,它是自定义视图,但由于某种原因,我无法从情节提要和视图的 awakeFromNib 更改它。

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.contentView.backgroundColor = .white
    }

G
Gent

iOS 8+

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}

n
nsinvocation

基于@Dj S 的回答,使用 Swift 3。这在 iOS 10 上效果很好。

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Background color
    view.tintColor = UIColor.black

    // Text Color
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.textColor = UIColor.white
}

I
ICL1901

我在 iOS 7.x 中有一个使用静态表格视图单元格的项目。 willDisplayHeaderView 不会触发。但是,这种方法可以正常工作:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSLog(@"%s", __FUNCTION__);
    CGRect headerFrame = CGRectMake(x, y, w, h);    
    UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];  
    headerView.backgroundColor = [UIColor blackColor];

V
Vinoth
 -(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 whiteColor]; // substitute your color here
             content.backgroundColor = color;
             [castView.textLabel setTextColor:[UIColor blackColor]];
        }
 }

m
mmtootmm

我认为这段代码还不错。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.whiteColor()
    headerView.backgroundView = backgroundView
    headerView.textLabel.text = "hello"
    return headerView
}

M
Maksymilian Wojakowski

在 iOS 7.0.4 中,我使用自己的 XIB 创建了一个自定义标头。在此之前没有提到任何内容。它必须是 UITableViewHeaderFooterView 的子类才能与 dequeueReusableHeaderFooterViewWithIdentifier: 一起使用,而且似乎该类对于背景颜色非常固执。所以最后我添加了一个名为 customBackgroudView 的 UIView(你可以使用代码或 IB 来完成),然后设置它的 backgroundColor 属性。在 layoutSubviews 中:我将该视图的框架设置为边界。 它适用于 iOS 7,不会出现任何故障。

// in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
// first child becomes contentView

// in MyTableHeaderView.h
@property (nonatomic, weak) IBOutlet UIView * customBackgroundView;

// in MyTableHeaderView.m
-(void)layoutSubviews;
{
    [super layoutSubviews];

    self.customBackgroundView.frame = self.bounds;
}
// if you don't have XIB / use IB, put in the initializer:
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    ...
    UIView * customBackgroundView = [[UIView alloc] init];
    [self.contentView addSubview:customBackgroundView];
    _customBackgroundView = customBackgroundView;
    ...
}


// in MyTableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyTableHeaderView * header = [self.tableView
                                          dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
    header.customBackgroundView.backgroundColor = [UIColor redColor];
    return header;
}

R
Ramesh Kumar

只需更改标题视图的图层颜色

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,    tableView.bounds.size.width, 30)] autorelease];
 headerView.layer.backgroundColor = [UIColor clearColor].CGColor
}

C
Cmar

如果有人需要 swift,请保留标题:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
    view.backgroundColor = UIColor.redColor()
    let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
    label.text = self.tableView(tableView, titleForHeaderInSection: section)
    view.addSubview(label)
    return view
}

P
Pokotuz

我通过控制台日志从 Xcode 收到消息

[TableView] 在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改为将具有所需背景颜色的自定义 UIView 设置为 backgroundView 属性。

然后我只是创建一个新的 UIView 并将其作为 HeaderView 的背景。这不是一个好的解决方案,但正如 Xcode 所说的那样容易。


I
Idrees Ashraf

就我而言,它是这样工作的:

let headerIdentifier = "HeaderIdentifier"
let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
header.contentView.backgroundColor = UIColor.white

L
Lukas Mohs

只需设置背景视图的背景颜色:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){         
  let tableHeader = view as! UITableViewHeaderFooterView        
  tableHeader.backgroundView?.backgroundColor = UIColor.white     
}

F
Farrukh Makhmudov

如果您使用的是自定义标题视图:

class YourCustomHeaderFooterView: UITableViewHeaderFooterView { 

override func awakeFromNib() {
    super.awakeFromNib()
    self.contentView.backgroundColor = .white //Or any color you want
}

}