Edit the text of an UILabel in Header

Hello,


I have a Table View Controller with a custom Header, customized in this function:


override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let shopHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 70))
        shopHeader.backgroundColor = UIColor(red: 209/255, green: 226/255, blue: 254/255, alpha: 1)
        let backShop = UIButton(frame: CGRect(x: 10, y: 20, width: 90, height: 30))
        backShop.setTitle("< back", for: .normal)
        backShop.setTitleColor(UIColor.black, for: .normal)
        backShop.titleLabel?.font = UIFont(name: "Jack Armstrong", size: 20)
        backShop.addTarget(self, action: #selector(pressed), for: .touchUpInside)
        shopHeader.addSubview(backShop)
        
        coins = UILabel(frame: CGRect(x: tableView.frame.size.width - 280, y: 20, width: 200, height: 30))
        coins.text = String(UserDefaults.standard.integer(forKey: "Total"))
        coins.font = UIFont(name: "Jack Armstrong", size: 17)
        coins.textAlignment = NSTextAlignment.right
        shopHeader.addSubview(coins)
        
        return shopHeader
    }


where coins is a global var. I have another function which job is change the text of the UILabel coins, but when I call this function the text doesn't change. What am I doing wrong? Is it mutable the Header of a TableView once it's loaded?

Replies

There is no reason why the header would be reloaded when you change coins.


So, when you change coins, reload the table, or better just the section where the header is.


You can do this:

- by calling directly when you change coins text

- by adding a didSet to coins property and call reload from didSet

- or better, create a didSet, but post a notification on change and have the class with the tableView subscribe to notification and reload section on receiving notification.


Another point. You say

where coins is a global var. I have another function which job is change the text of the UILabel coins


Why do you make coins a global var and set it in the table header ?

Does the change goes through the change of userDefaults ? Otherwise, if you change the coins.text in a func but reset it with userDfaults in tableView, you will run into problems


Why not just have a var for the text.

Then the didset would be on this String only.

You have two problems.


First you need to call something like

[myTableView reloadData]; // this is ObjectiveC, I don't do Swift

where myTableView is a reference to the tableView


Second (as mentioned by Claude31), in your method viewForHeaderInSection you are recreating "coins" and resetting the text by:

coins.text = String(UserDefaults.standard.integer(forKey: "Total"))

So perhaps you should eliminate these lines in that method:

coins = UILabel(frame: CGRect(x: tableView.frame.size.width - 280, y: 20, width: 200, height: 30))

coins.text = String(UserDefaults.standard.integer(forKey: "Total"))

coins.font = UIFont(name: "Jack Armstrong", size: 17)

coins.textAlignment = NSTextAlignment.right

If it is a global, all you need in viewForHeaderInSection:

shopHeader.addSubview(coins)