0

With Swift 3, I am using a subclass of UITableViewHeaderFooterView (called HeaderView) for the header sections on my TableView.

After dequeueing HeaderView, I customise it by (1) setting the textLabel.textColor = UIColor.red, and (2) adding a subview to it.

When the application first loads, the table view loads up the headers but they have (what I assume is) the 'default' view (with textLabel.textColor being grey, and without my added subview). When I start scrolling and it starts dequeueing more HeaderViews, then the HeaderViews start coming up correctly, until there are eventually no more 'default' formatted HeaderViews.

Subsequent loads of the app no longer shows the 'default' view.

Alternatives considered

  • I know that this could be done by making my HeaderView a subclass of UITableViewCell and customising it from the Storyboard, but that seems like more of a workaround to use a prototype cell when there is a UITableViewHeaderFooterView class that was designated for headers
  • Similarly it could be done with a XIB file, but even in Xcode 8 when creating a subclass of UITableViewHeaderFooterView it doesn't allow you to create an XIB file (so there must be some reason..)

Any comments/answers explaining why this is happening and how to resolve it are really appreciated!

UPDATE

As requested I've added in the code to show what I've done- you can recreate the problem with the code below and the usual setting up a TableViewController in the Storyboard (Swift 3, Xcode 8.2, Simulating on iOS 10.2 for iPhone 7)

ListTableViewController.swift

import UIKit

class ListTableViewController: UITableViewController {

    // List of titles for each header
    var titles: [String] {
        var titles = [String]()
        for i in 1...100 {
            titles.append("List \(i)")
        }
        return titles as [String]
        
    }
    
    // Register view for header in here
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(ListHeaderView.self, forHeaderFooterViewReuseIdentifier: "Header")
    }

    // Table view data source
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let dequeuedCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header")
        if let cell = dequeuedCell as? ListHeaderView {
            cell.title = titles[section]
        }
        return dequeuedCell
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return titles.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
}

ListHeaderView.swift

import UIKit

class ListHeaderView: UITableViewHeaderFooterView {

    var title: String? {
        didSet {
            updateUI()
        }
    }
    
    private func updateUI() {
        textLabel?.textColor = UIColor.red
        textLabel?.text = title!
        let separatorFrame = CGRect(x: 0, y: frame.height-1, width: frame.width, height: 0.25)
        let separator = UIView(frame: separatorFrame)
        separator.backgroundColor = UIColor.red
        contentView.addSubview(separator)
    }

}

Here is a screen shot of when the grey headers (screen is full of them upon initial load) and the customised red headers which start to appear upon scrolling.

Header Views not showing customisations at first

Community
  • 1
  • 1
TL77
  • 83
  • 1
  • 8

2 Answers2

1

For anyone interested, seems like this is a bug for which the best solution at this stage is to configure properties such as textColor on the header view in the tableView delegate method willDisplayHeaderView. Doing so 'last minute' just before the view appears allows you to override whatever configurations the system tries to force on the font etc.

Credit to answer found here Troubles with changing the font size in UITableViewHeaderFooterView

Community
  • 1
  • 1
TL77
  • 83
  • 1
  • 8
0

Use this below code

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let dequeuedCell : ListHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "Header") as? ListHeaderView

        cell.title = titles[section]
    cell.tittle.textcolor = uicolor.red
    return dequeuedCell
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51