I have a simple tableViewCell build in interface builder. It contains a UIView which contains an image. Now, when I select the cell, the default blue selection background is shown, but the backgroundColor of my UIView is gone.
My UITableViewCell's implementation file doesn't do anything special. It just init's & returns self and all I do in setSelected is call super.
How do I get my UIView backgroundColor to show when the tableView is selected?
Summary
This solution let's you lock some of a cell's background colors, while the remainder are controlled by system behaviour.
Based on mientus' answer, I have created a solution which allows you to specify which views should keep their background color.
This still allows other cell subviews to have their background removed on highlighting/selection, and is the only solution which works in our case (two views needing a permanent background).
I used a protocol-oriented approach, with a
BackgroundLockable
protocol containing the list of views to lock, and running a closure while keeping the colors:Then I have a subclass of
UITableViewCell
, which overrides highlighting and selection to run the protocol's closure around calling the default (super) behaviour:Now I just have to subclass
LockableBackgroundTableViewCell
or use theBackgroundLockable
protocol in a cell class to easily add locking behaviour to some cells!Ok, loosing the background color of a UIView class is normal behavior when its in a selected tableviewcell. I couldn't figure out how to prevent that. Now I've just replaced the UIView with an UIImageView containing a stretched 1x1 white pixel. Ugly imo, but it works.
Brooks has a great explanation for why this happens, but I think I have a better solution.
In your subview, override
setBackgroundColor:
to what ever color you want. The setter will still be called, but only your color specified will be enforced.Add this to your UITableViewCell