Swift:无法以编程方式编辑自定义表格单元格的属性 [英] Swift: Can't edit properties of custom table cell programmatically
问题描述
我正在尝试更改自定义表格单元格内视图的颜色,但是有一个插座,可以正常工作.我可以更改此视图的其他属性,例如.isHidden
,但.backgroundColor
似乎不起作用.知道我在做什么错吗?
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
(名为:绿色")可在应用程序的其他部分中使用,但我也无法使用它来更改文本的颜色.我分配的颜色类型错误吗?情节提要中的值是否只是覆盖了此内容?如果是这样,我该如何阻止这种情况的发生?将其更改为= .red
也不起作用.
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.
代码如下:
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
}
推荐答案
对此进行检查,这可能是一个问题. UITableViewCell textLabel颜色不变 注意:无法评论声名狼藉,这就是为什么将其发布为答案.
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.
这篇关于Swift:无法以编程方式编辑自定义表格单元格的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!