如何在TableView中更改区域标题的颜色?

23

这是我目前的情况。

输入图像描述

我如何引用它,以便可以更改文本颜色以匹配我的索引列表? sectionForSectionIndexTitle 在添加正确的部分标题方面效果良好,但如何访问标题元素呢?

或者这是不可能的,我需要重新绘制视图并通过viewForHeaderInSection添加它吗?


是的,您需要在viewForHeaderInSection中添加标签。 - Janmenjaya
兄弟,你是怎么在右边添加字母的? - user8169082
它被称为索引列表。这是一个视频https://www.youtube.com/watch?v=xYSKHna1KJk 。或者另一个具有很好教程的链接。http://www.edumobile.org/ios/indexed-table-views-in-swift/ - AMAN77
6个回答

65

您可以使用UITableViewDelegate的方法之一

Swift3及以上版本

  func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .white
        headerView.backgroundView?.backgroundColor = .black
        headerView.textLabel?.textColor = .red
    }
}

Objective-C

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
        UITableViewHeaderFooterView * headerView = (UITableViewHeaderFooterView *) view;
        headerView.textLabel.textColor  = [UIColor RedColor];  
    }
}

为了参考,我从这里采用了模板答案。


可能是我在SO上最喜欢的答案。 - Ashley Mills

10

一行代码的解决方案(使用可选链):

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

这与@Anbu.karthik的解决方案有何不同? - Ashley Mills
1
结果是一样的,但这个使用了可选链,因此只有一行代码,而不是一个 if let 条件语句。我会更新答案来指出这一点。 - Federico Zanetello
适用于手机和平板电脑,但在运行Mac Catalyst时更改页脚颜色时遇到了问题。 - Kurt L.

3

自定义标题:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let title = UILabel()
    title.font = UIFont(name: "SFUIDisplay-Light", size: 13)!
    title.textColor = UIColor.redColor()

    let header = view as! UITableViewHeaderFooterView
    header.textLabel!.font=title.font
    header.textLabel!.textColor=title.textColor
    header.contentView.backgroundColor = UIColor.whiteColor()
}

3

Swift解决方案

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

1
我会使用Appearance()代理类。通常我会在AppDelegate中的一个函数中添加它们,并在didFinishLaunching中调用它们。
private func setupApperances() {
    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .red
}

0

你可以自己制作自己的章节标题(页眉/页脚)视图,而且非常容易。

class BlackTableViewHeaderFooterView : UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = .black

        textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
        textLabel?.numberOfLines = 0
        textLabel?.textColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TableViewController : UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(BlackTableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "\(BlackTableViewHeaderFooterView.self)")
        // do other setup
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(BlackTableViewHeaderFooterView.self)")
        header.textLabel?.text = "" // set your header title
        return header
    }
}

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