Search code examples
iosuitableviewswiftuicolor

cellForRowAtIndexPath and prepareForSegue return different label colors


I have a custom table view cell. Inside the cell is a label I added from the object library and I gave it a viewWithTag value of 1. The label in the table view cell has a yellow background color. Here's the code:

Within cellForRowAtIndexPath I have

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)

Now, within prepareForSegue I have

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

Therefore I'm getting Optional(UIDeviceRGBColorSpace 0.980392 0.776471 0 1) and Optional(UIDeviceWhiteColorSpace 0 0) from the same label's backgroundColor property according to my understanding. Shouldn't it be the same RGB value? What am I missing here? If this is the wrong way to access the label's backgroundColor property, how would I go about accessing it properly?


Solution

  • This seems to be due to the color change that happens on selection. If you set the selection style to "none" for your cell, the correct color is reported in prepareForSegue.