Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在tableview中更改章节标题的颜色?_Ios_Swift_Tableview_Uitableviewsectionheader - Fatal编程技术网

Ios 如何在tableview中更改章节标题的颜色?

Ios 如何在tableview中更改章节标题的颜色?,ios,swift,tableview,uitableviewsectionheader,Ios,Swift,Tableview,Uitableviewsectionheader,这是我目前的情况 我如何引用它,以便更改文本颜色以匹配索引列表?sectionForSectionIndexTitle可以很好地添加正确的节标题,但是如何准确地访问title元素呢 还是不可能,我需要重新绘制视图,并将其添加到viewForHeaderInSection?自定义标题: override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: In

这是我目前的情况

我如何引用它,以便更改文本颜色以匹配索引列表?sectionForSectionIndexTitle可以很好地添加正确的节标题,但是如何准确地访问title元素呢

还是不可能,我需要重新绘制视图,并将其添加到viewForHeaderInSection

自定义标题:

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()
}

您可以使用
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
    }
}
目标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];  
    }
}
作为参考,我从

一个线性解决方案(使用可选链接)中获取了模型答案:


您可以创建自己的节标题(页眉/页脚)视图,这很容易

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
    }
}
我将使用Appearance()代理类。我通常将它们添加到AppDelegate的函数中,并将它们称为didFinishLaunching

private func setupApperances() {
    UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .red
}

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
}

是的,您需要在viewForHeaderInSection中添加标签。Hye man您是如何添加右侧的字母表的?它称为索引列表。这是视频。或者是一个很棒的教程链接。可能是我最喜欢的答案。这与@Anbu.karthik的解决方案有何不同?^^结果是相同的,但这一个使用了可选的链接,因此产生了一行,而不是if let条件下的
。我将更新答案,指出这一点适用于phone和pad,但在Mac catalyst上运行时无法更改页脚的颜色
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}