Uiview Backgroundcolor Disappears When Uitableviewcell Is Selected

UIView backgroundColor disappears when UITableViewCell 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

  1. setHighlighted
  2. touchesEnded
  3. layoutSubviews
  4. willSelectRowAtIndexPath (delegate side)
  5. setSelected (!!! this is where all your view background colors are told to disappear)
  6. didSelectRowAtIndexPath (delegate side)
  7. setSelected (again) (Interestingly background colors not cleared on this call. What strangeness is going on inside that super method?)
  8. layoutSubviews (again)

So your options are to

  1. Override - (void) setSelected:(BOOL) selected animated:(BOOL) animated; without calling [super setSelected:selected animated:animated]. This will give you the most technically correct implementation because a) the code is wrapped up inside the UITableViewCell subclass and b) because it is only called when needed (well twice when needed, but maybe there is a way around that). The down side is you'll have to re-implement all the necessary functions (as opposed to unnecessary color clearing functions) of setSelected. Now don't ask me how to properly override setSelected just yet. Your guess is as good as mine for now (be patient, I'll edit this answer once I figure it out).
  2. Re-assert the background colors in didSelectRowAtIndexPath. This is not so great because it puts what should be instance code outside the instance. It has the upside that it is only called when it is needed, as opposed to ...
  3. Re-assert the background colors in layoutSubviews. This is not great at all because layoutSubviews is called like A MILLION times! It is called every time the table refreshes, every time it scrolls, every time you grandmother gets a perm... like seriously, a million times. That means there is a lot of unnecessary background re-assertions and a lot of extra processing overhead. On the bright side it puts the code inside the UITableViewCell subclass, which is nice.

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)

UIView disappears when UITableViewCell is tapped

I've managed to find a solution as described in this answer but in my case in obj-c.

Apparently what happens when a UITableViewCell gets selected or highlighted is that automatically all enclosing background-colors of views are removed (I think it is [UIColor clearColor]).

What I'm doing is taking the color of my views before doing the super method call to setSelected or setHighlighted and then immediately set the color as background.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

// Gets route view background color before doing setSelected animation
UIColor *routeViewBkgColor = self.routeView.backgroundColor;
[super setSelected:selected animated:animated];

// Set route view background color after setSelected animation
self.routeView.backgroundColor = routeViewBkgColor;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

// Gets route view background color before doing setSelected animation
UIColor *routeViewBkgColor = self.routeView.backgroundColor;
[super setHighlighted:highlighted animated:animated];

// Set route view background color after setSelected animation
self.routeView.backgroundColor = routeViewBkgColor;
}

In this way I'm 100% able to get the desired effect of keeping selection behaviour without losing the background color for all my views.

UITableViewCell subview disappears when cell is selected

UITableViewCell changes the background color of all sub views when cell is selected or highlighted ,You can Solve this problem by overriding Tableview cell's setSelected:animated and setHighlighted:animated and resetting view background color.

In Objective C :

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
UIColor *color = self.yourView.backgroundColor;
[super setSelected:selected animated:animated];

if (selected){
self.yourView.backgroundColor = color;
}
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
UIColor *color = self.yourView.backgroundColor;
[super setHighlighted:highlighted animated:animated];

if (highlighted){
self.yourView.backgroundColor = color;
}
}

In Swift 3.1 :

override func setSelected(_ selected: Bool, animated: Bool) {
let color = yourView.backgroundColor
super.setSelected(selected, animated: animated)

if selected {
yourView.backgroundColor = color
}
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
let color = yourView.backgroundColor
super.setHighlighted(highlighted, animated: animated)

if highlighted {
yourView.backgroundColor = color
}
}

Why do all backgrounds disappear on UITableViewCell select?

What is happening is that each subview inside the TableViewCell will receive the setSelected and setHighlighted methods. The setSelected method will remove background colors but if you set it for the selected state it will be corrected.

For example if those are UILabels added as subviews in your customized cell, then you can add this to the setSelected method of your TableViewCell implementation code:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

self.textLabel.backgroundColor = [UIColor blackColor];

}

where self.textLabel would be whatever those labels are that are shown in the picture above

I'm not sure where your adding your selected view, I usually add it in the setSelected method.

Alternatively, you can subclass the UILabel and override the setHighlighted method like so:

-(void)setHighlighted:(BOOL)highlighted
{
[self setBackgroundColor:[UIColor blackColor]];
}

UITableViewCell subclassed, when selected disappears UIImageView

You custom view looks to be just a colored UIView. When a UITableViewcell is selected, all subviews inside of it get their background color changed on highlight. You would need to have something more than just a UIView with a set background color. You would need a UIImageView, or some such thing.

UITableViewCell Selected Background Color on Multiple Selection

Swift 4.2

For multiple selections you need to set the UITableView property allowsMultipleSelection to true.

myTableView.allowsMultipleSelection = true

In case you subclassed the UITableViewCell, you override setSelected(_ selected: Bool, animated: Bool) method in your custom cell class.

 override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

if selected {
contentView.backgroundColor = UIColor.green
} else {
contentView.backgroundColor = UIColor.blue
}
}


Related Topics



Leave a reply



Submit