selection color for a UITableViewCell
Solution 1
Just don't subclass UITableViewCell and use the default behavior. You can fully customize a cell without any subclassing.
Read this article for more details.
Solution 2
Add this code in your tableview cellForRowAtIndexPath method and just change expected color for UITableViewCell selection style.
//-------------------------------------------------------------------------
//background selected view
UIView *viwSelectedBackgroundView=[[UIView alloc]init];
viwSelectedBackgroundView.backgroundColor=[UIColor colorWithRed:124.0/255.0 green:202.0/255.0 blue:227.0/255.0 alpha:1.0];
cell.selectedBackgroundView=viwSelectedBackgroundView;
//-------------------------------------------------------------------------
Solution 3
If you have subclassed a UITableViewCell, then you can customise the elements of the cell by overriding the following:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
if(highlighted) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor clearColor];
}
[super setHighlighted:highlighted animated:animated];
}
Sophie Alpert
I'm a software engineer. I build things to help people. Currently building React at Facebook, previously at Khan Academy.
Updated on June 04, 2022Comments
-
Sophie Alpert almost 2 years
If I have a custom
UITableViewCell
that doesn't use thetextLabel
built in to the cell but instead does its own drawing, how can I change the appearance of thecontentView
on selection, like it does automatically for the default text (customizable by setting theselectedTextColor:
)?If I change
tableView:willSelectRowAtIndexPath:
, then it only updates after the blue selection background is up, but not while it's animating, like I want. -
Sophie Alpert almost 15 yearsThat's in fact what I was doing, but your link still pointed me in the right direction. I needed to change the highlightedTextColor for the UILabel.
-
Thomas almost 15 yearsWhen you talked about "own drawing" I immediately thought subclassing. My bad. Glad you find a solution