The UITableViewCell sub-screen disappears when selected (but the content in this view is visible)

This is the .m file of my UITableViewCell.

#import "ContentCardTableViewCell.h" #import <QuartzCore/QuartzCore.h> @implementation ContentCardTableViewCell - (void)awakeFromNib { // Initialization code [self setBackgroundColor:[UIColor clearColor]]; } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; if (highlighted) { [self setBackgroundColor:[UIColor clearColor]]; // Recover backgroundColor of subviews. } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { [self setBackgroundColor:[UIColor clearColor]]; // Recover backgroundColor of subviews. } } @end 

But one view in this UITableViewCell disappears when selected. I tried this and this and much more, but nothing helped. Is something missing?

+6
source share
2 answers

I had a similar problem.
If I set selectionStyle to UITableViewCellSelectionStyleNone , it works fine, the problem is resolved. i.e

in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

add
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

before returning the cell. Hope this works.

+7
source

you forgot to restore state after deselecting a cell

 if (selected) { [self.contentView setBackgroundColor:[UIColor clearColor]]; // Recover backgroundColor of subviews. }else{ [self.contentView setBackgroundColor:[UIColor whiteColor]] } 

PS: he prefers to set the color of cell.contentView

+2
source

Source: https://habr.com/ru/post/988710/


All Articles