Search code examples
iphoneuitableviewios4uilabeluicolor

UILabel backgroundColor in UITableViewCell always reverts to initial value


In my app, I have a UITableViewCell which is used to display a background color setting. In the detailTextLabel, it displays the name of the color with the background set to the actual color, e.g. [UIColor cyanColor]. Note that I am setting the background of the detailTextLabel only, not the whole UITableViewCell. When users tap on the cell they are taken to another UITableView which lets them choose a color, and when they return to the previous UITableView the backgroundColor of the UILabel is updated to the new color.

The problem is, whenever I return to the initial UITableView, the UILabel's backgroundColor updates momentarily and then returns to the initial color. I cannot find out why it would be reverting. Any suggestions?

Thank you!


Solution

  • Some state-based properties are set by the table view; I believe that background color is one of them. In other words, the table view is changing the background color of detailTextLabel, probably as part of unhighlighting the selection.

    After the table view sets state-based properties, the table delegate is given a final chance to update the appearance of each cell. This is done in the delegate's tableView:willDisplayCell:forRowAtIndexPath: method. Perhaps if you set the background color of detailTextLabel in this method your problem will go away.