LucioB
LucioB

Reputation: 1764

UIView disappears when UITableViewCell is tapped

In my iOS application I'm currently struggling with an issue on a custom UITableViewCell that I've designed in the storyboard.

The issue appears when I do tap on a table view row that is rendered using this particular UITableViewCell.

This is the default look of a row rendered with this UITableViewCell:

default look of a row rendered with UITableViewCell

This is how it appears after I tap on it:

how it appears after I tap

As you can see the green line is gone and remains invisible till the row is tapped. As soon as the row is released it comes back to default look and everything is fine.

This is what I was expecting to see:

expected result

This is the UITableViewCell structure as shown into the storyboard. RouteView is the green line UIView item:

UITableViewCell structure as shown into the storyboard

I also add the attributes section of the storyboard for the UIView object:

attributes section of the storyboard for the UIView

I have a class attached to the UITableViewCell that handles layouts of the objects through IBOutlets, while the tap on the row is handled at UITableView level.

I change the background color of the UIView based on some conditions but it can be green, red and blue, never transparent. Is it something related to this?

How can I solve this issue? Thank you in advance.

Upvotes: 0

Views: 1081

Answers (3)

LucioB
LucioB

Reputation: 1764

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.

Upvotes: 3

stepik21
stepik21

Reputation: 2640

Both of current answers are correct, but just in case you are working in Objective-C instead of swift.

You have to use cell.selectionStyle = UITableViewCellSelectionStyleNone;

Or, if you want to have efect of "click", than you have to use

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Hope it helps! :-)

Upvotes: 1

Kostas Tsoleridis
Kostas Tsoleridis

Reputation: 924

You probably need to set the selection style of the cell to none. You can do this either programmatically as Nirav D mentioned or through your storyboard (see image).

enter image description here

Upvotes: 0

Related Questions