简体   繁体   中英

How to change the section header text color in UITableView

I've been actively looking for a solution to do this, and the closest I got was this:

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

I put this function in my TableViewController, as I understand it's a UITableViewDelegate method. For context, the UITableViewController I'm using is embedded into a Container View within a UIView. Nothing happens. Why is this?

How did you register your header view cell in Section of TableView?? Please show more code

I recommend that you should implement your table header view cell in Section like the following steps:

Step 1 : Create YourTableHeaderViewCell

在此处输入图像描述

Step 2 : Add text label in YourTableHeaderViewCell.xib

在此处输入图像描述

Step 3 : Create the outlet text label in YourTableHeaderViewCell.swift

在此处输入图像描述

It became like:

class YourTableHeaderViewCell: UITableViewCell {

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

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

Step 4 : Setup YourViewController and Register your table header view cell:

在此处输入图像描述

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) {

    }
}

Make sure you implement:

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

And make you sure you are not implementing this at the same time:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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