【发布时间】:2019-03-23 16:15:58
【问题描述】:
我正在尝试更改自定义表格单元格中视图的颜色,并且我有一个出口,它可以工作。我可以更改此视图的其他属性,例如 .isHidden 但 .backgroundColor 似乎不起作用。知道我做错了什么吗?
UIColor(named: "Green") 适用于应用程序的其他部分,但我也无法使用它更改文本的颜色。我是否分配了错误的颜色类型?故事板中的值是否只是覆盖了这个?如果是这样,我怎么能阻止这种情况发生?将其更改为= .red 也不起作用。
代码如下:
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
}
【问题讨论】:
-
嗯,感谢@GaloTorresSevilla,视图是在情节提要中创建的,并且可以正确渲染。问题是颜色没有变化。我有一个视图的出口,所以我认为这会改变它的背景颜色。例如,我可以使用 .isHidden 使用插座来隐藏它。 dropbox.com/s/8z95rb2ozdgmfnt/…关于该项目的任何其他细节会有所帮助吗?
-
不要在表的数据源中实例化视图。使用可重用单元格时,数据源应尽可能轻量级。它应该做的就是向单元格注入数据。在别处做所有繁重的工作。在选定背景的情况下,在单元格本身中对其进行编码。
-
考虑制作两个单元格,每个单元格都以自己的方式设置样式,并根据数据将数据源中所需的单元格出列。尽量不要为数据源中的单元格设置样式,尤其是在单元格的层次结构中切换视图,自动布局会讨厌你,你的滚动性能可能会受到明显影响,而且肯定会出现问题。
-
看来这就是我必须做的,谢谢。
标签: swift uitableview uiview uibackgroundcolor