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?
The problem here is that the [super] implementation of
- (void) setSelected:(BOOL) selected animated:(BOOL) animated;
sets all the background colors in the UITableViewCell to rgba(0,0,0,0). Why? Perhaps to make us all sweat?
It is not that entire views disappear (as evidenced by the fact that if you change the views layer border properties, those are retained)
Here is the sequence of function calls that results from touching a cell
So your options are to
Unfortunately re-asserting the background colors in setHighlighted does nothing because setHighlighted is called before all the background colors get set to [r:0 b:0 g:0 a:0] by the first call to setSelected.
//TODO: Give a great description of how to override setSelected (stay tuned)
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
UIColor *backgroundColor = self.channelImageView.backgroundColor;
[super setHighlighted:highlighted animated:animated];
self.channelImageView.backgroundColor = backgroundColor;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
UIColor *backgroundColor = self.channelImageView.backgroundColor;
[super setSelected:selected animated:animated];
self.channelImageView.backgroundColor = backgroundColor;
}
When your UITableViewCell
is selected, there are two states you should pay attention to: Highlighted
and Selected
.
So, for scenarios that you have a custom cell class which is subclass of UITableViewCell
, you can easily override these two methods to avoid this situation(Swift):
class MyCell: UITableViewCell {
@IBOutlet var myView: UIView!
override func setHighlighted(highlighted: Bool, animated: Bool) {
let myViewBackgroundColor = myView.backgroundColor
super.setHighlighted(highlighted, animated: animated)
myView.backgroundColor = myViewBackgroundColor
}
override func setSelected(selected: Bool, animated: Bool) {
let myViewBackgroundColor = myView.backgroundColor
super.setSelected(selected, animated: animated)
myView.backgroundColor = myViewBackgroundColor
}
}
Previously I have done as @P5ycH0 said (1x1 image stretched), but following @Brooks I figured that overriding -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
in my custom UITableViewCell
implementation and resetting the the background colors after calling [super setHighlighted:highlighted animated:animated];
keeps my background colors when the cell is selected/highlighted
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
myView.backgroundColor = myColor;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With