ios - cellForRowAtIndexPath 和 prepareForSegue 返回不同的标签颜色

标签 ios uitableview swift uicolor

我有一个自定义表格 View 单元格。单元格内部是我从对象库添加的标签,我给它的 viewWithTag 值为 1。表格 View 单元格中的标签具有黄色背景色。这是代码:

cellForRowAtIndexPath 我有

let label = cell.viewWithTag(1) as! UILabel
label.backgroundColor = yellowColor // yellowColor is a regular UIColor I defined elsewhere
println(label.backgroundColor) // returns Optional(UIDeviceRGBColorSpace 0.980392 0.776471 0 1)

现在,在 prepareForSegue 我有

let cell = tableView.cellForRowAtIndexPath(tableView.indexPathForSelectedRow()!)
let labelBackgroundColor = cell?.viewWithTag(1) as! UILabel).backgroundColor
println(labelBackgroundColor) // returns Optional(UIDeviceWhiteColorSpace 0 0)

因此,根据我的理解,我从同一标签的 backgroundColor 属性中获取 Optional(UIDeviceRGBColorSpace 0.980392 0.776471 0 1)Optional(UIDeviceWhiteColorSpace 0 0)。它不应该是相同的RGB值吗?我在这里错过了什么?如果这是访问标签的 backgroundColor 属性的错误方法,我将如何正确访问它?

最佳答案

这似乎是由于选择时发生的颜色变化。如果您将单元格的选择样式设置为“无”,则会在 prepareForSegue 中报告正确的颜色。

关于ios - cellForRowAtIndexPath 和 prepareForSegue 返回不同的标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720241/

相关文章:

ios - Accordion 风格的平滑 UITableView 单元格扩展

ios - Swift:在 UITableViewCell 中异步加载图像 - 自动布局问题

ios - 带有@Binding可选属性的SwiftUI View的初始化似乎模棱两可

ios - ContiguousArrayStorage 没有实现 methodSignatureForSelector

ios - 在UITextField中输入值,使用实体类中的方法,需要值在TableView中显示

ios - 有没有办法观察 UIViewPropertyAnimator 中 fractionComplete 的变化

ios - 为什么我的 UIViewController 没有正确发布?

ios - Tableview header 标签在中心对齐

ios - 当按下不同的按钮时,按下按钮时发送的数据仍然存在

swift - 二元运算符 + 不能应用于两个 int 操作数