繁体   English   中英

如何更改 UITableView 中 header 部分的文本颜色

[英]How to change the section header text color in UITableView

我一直在积极寻找解决方案来做到这一点,我得到的最接近的是:

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

我把这个 function 放在我的 TableViewController 中,据我所知这是一个 UITableViewDelegate 方法。 对于上下文,我使用的 UITableViewController 嵌入到 UIView 内的容器视图中。 什么都没发生。 为什么是这样?

您是如何在 TableView 部分中注册 header 视图单元格的? 请显示更多代码

建议您应该按照以下步骤在 Section 中实现表 header 视图单元格:

第 1 步:创建YourTableHeaderViewCell

在此处输入图像描述

第 2 步:在 YourTableHeaderViewCell.xib 中添加文本label

在此处输入图像描述

第 3 步:在 YourTableHeaderViewCell.swift 中创建出口文本label

在此处输入图像描述

变成了:

class YourTableHeaderViewCell: UITableViewCell {

    // MARK: - Outlets
    @IBOutle weak var textLabel: UILabel!

    // MARK: - Variables
    static var identifier = "YourTableHeaderViewCell"
}

第 4 步:设置YourViewController注册您的表 header 视图单元:

在此处输入图像描述

class YourViewController: UIViewController {

    // MARK: - View Cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        setupTableView()
    }

    private func setupTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.clear
        tableView.register(UINib.init(nibName: YourTableViewHeaderView.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewHeaderView.identifier) // register your table header view cell
        tableView.register(UINib.init(nibName: YourTableViewCell.identifier, bundle: nil), forCellReuseIdentifier: YourTableViewCel.identifier) // register your table view cell
    }
}

// MARK: - UITableViewDataSource
extension YourViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return // number of your sections
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return // number of rows in sections
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewCell.identifier, for: indexPath) as? YourTableViewCell {
            return cell
        }
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewHeaderView.identifier) as? YourTableViewHeaderView {
            cell?.textLabel?.textColor = UIColor.white // Change the text color here
            return cell.contentView
        }
        return UIView()
    }
}

// MARK: - UITableViewDelegate
extension YourViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

确保您实施:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "title"
}

并确保您没有同时实现此功能:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    <#code#>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM