Kevin Vugts Kevin Vugts - 4 months ago 33
iOS Question

Change UILabel on SelectedRow

I am trying to change the label color of the cell in a tableView that is selected.
I am trying to reach this with the following code:

TextLabel.HighlightedTextColor = UIColor.White;

Unfortunately this does not work and will keep his original color that I filled in from the StoryBoard.

I will provide a picture of the full code. The blue background color does work but the label color does not.

enter image description here

Looking forward for your help, thank you very much.



Subclass your UITableViewCell and set it there:

class YourTableViewCell: UITableViewCell

    @IBOutlet weak var myLabel: UILabel!

    override func setSelected(_ selected: Bool, animated: Bool) {
        if(selected) {            
            self.myLabel.textColor = UIColor.white
        } else {
            self.myLabel.textColor = UIColor(red: 84.0/255.0, green: 199.0/255.0, 252.0/255.0)

Then Your cellForRow method could look like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = ... as! YourTableViewCell 

    /// you don't need to set the highlight color here.