I am trying to change the colour of a view inside a custom table cell and I have an outlet to it, which works. I can change other properties of this view, like .isHidden
but .backgroundColor
doesn't seem to work. Any idea what I'm doing wrong?
UIColor
(named: "Green") works in other parts of the app, but I can't change the colour of the text with it either. Am I assigning the wrong type of color? Are the values from the storyboard just overwriting this? If so, how could I stop that from happening? Changing it to = .red
doesn't work either.
Here's the code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChapterCell") as! ChapterCell
smallLabel(cell.chapterLabel, 18)
cell.keepSubviewBackground = true
if chapters[indexPath.row].completed == true {
cell.chapterNumber.isHidden = true
cell.chapterTick.isHidden = false
cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
cell.chapterLabel?.textColor = UIColor(named: "Green")
cell.chapterNumberContainer.backgroundColor = UIColor(named: "Green")
} else {
cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
cell.chapterNumber?.text = "#\(indexPath.row + 1)"
cell.chapterTick.isHidden = true
cell.chapterNumber.isHidden = false
cell.chapterNumberContainer.backgroundColor = UIColor(named: "Eggshell")
}
if chapters[indexPath.row].locked == true {
cell.chapterLabel?.alpha = 0.3
cell.chapterNumberContainer?.alpha = 0.3
} else {
cell.chapterLabel?.alpha = 1
cell.chapterNumberContainer?.alpha = 1
}
let cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
cell.selectedBackgroundView = cellBGView
return cell
}
Check this,It can be an issue here.
UITableViewCell textLabel color not changing
Notes:Can't comment for less reputation.That's why posted it as an answer.
Thanks a lot for all the suggestions, I ended up going with the solution that bsod suggested and I'd like to provide more details for anyone who needs this in the future. Rob's solution didn't work for me.
- Duplicate the first custom cell inside the tableview and stylise it however you want.
- Create a second class and assign it to the second cell, as well as a new identifier. You can also create any outlets you need in this second class and they can have the same name.
In the cellForRowAt indexPath method, I've used an if statement to dequeue the custom cells based on whether a chapter is completed or not and then style them accordingly. This repeats some code but the cell has to be type cast to work (if anyone can think of a way to do this better, I'm open to alternatives).
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if chapters[indexPath.row].completed == false {
let cell = tableView.dequeueReusableCell(withIdentifier: "RegularCell") as! RegularCell
cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
cell.chapterNumber?.text = "#\(indexPath.row + 1)"
if chapters[indexPath.row].locked == true {
cell.chapterLabel?.alpha = 0.3
cell.chapterNumberContainer?.alpha = 0.3
} else {
cell.chapterLabel?.alpha = 1
cell.chapterNumberContainer?.alpha = 1
}
let cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
cell.selectedBackgroundView = cellBGView
smallLabel(cell.chapterLabel, 18)
cell.keepSubviewBackground = true
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "CompletedCell") as! CompletedCell
cell.chapterLabel?.text = chapters[indexPath.row].generateTitle()
let cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 1.00, green: 1.00, blue: 1.00, alpha: 0.1)
cell.selectedBackgroundView = cellBGView
smallLabel(cell.chapterLabel, 18)
cell.keepSubviewBackground = true
return cell
}