Search code examples
iosswiftxcode

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?


Solution

  • 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

    enter image description here

    Step 2: Add text label in YourTableHeaderViewCell.xib

    enter image description here

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

    enter image description here

    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:

    enter image description here

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