【发布时间】:2016-02-19 21:10:40
【问题描述】:
我的 Swift 应用程序遇到了一个奇怪的问题。我正在尝试使用我创建的自定义单元格创建UITableViewCell。
单元格中有一个空标签和一个文本标签。通过将backgroundColor 设置为一些 R、G、B 颜色来简单地为空标签着色。
但是,当我在表格中选择和取消选择行时,标签的背景颜色会消失。这种情况会发生,直到我将单元格滚动出视图并再次回到视图中,它再次向我显示颜色。
这里有一些屏幕截图来说明正在发生的事情:
这是我选择颜色时的样子 - 它似乎将标签背景颜色更改为透明。它不应该这样做
当然,我不希望这种情况发生。目的是让标签颜色保持不变。
这是我的 cellForRowAtIndexPath 代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ScenesTableCell
cell.sceneNameLabel.text = scenesArr[indexPath.row].sceneName
let red = scenesArr[indexPath.row].sceneCol[0]
let green = scenesArr[indexPath.row].sceneCol[1]
let blue = scenesArr[indexPath.row].sceneCol[2]
let brightness = scenesArr[indexPath.row].sceneBrightnessMultiplier
cell.colourIndicatorLabel.layer.backgroundColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(brightness)).CGColor
cell.colourIndicatorLabel.layer.cornerRadius = 5
cell.colourIndicatorLabel.layer.borderWidth = 1
cell.colourIndicatorLabel.layer.borderColor = UIColor(red: 77.0/255.0, green: 146.0/255.0, blue: 203.0/255.0, alpha: 1.0).CGColor
}
请注意,我也尝试了以下代码行来更改背景颜色,但是发生了同样的事情,但它填充了圆角边框之外:
cell.colourIndicatorLabel.backgroundColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(brightness))
我真的很感谢这里的一些帮助!我知道我不太擅长在 SO 上提问,所以如果您有任何问题,请提问! ;)
【问题讨论】:
-
可以使用
cell.colourIndicatorLabel.backgroundColor,为什么还要使用cell.colourIndicatorLabel.layer.backgroundColor? -
@penatheboss 我已经设置了一个角半径,当我在这个庄园中设置背景颜色时,颜色溢出了角落。无论哪种方式,当我选择行时,我都会遇到同样的透明度问题。如果可以解决透明度问题,我很乐意使用任何一种方法
标签: ios uitableview colors custom-cell