selection color for a UITableViewCell
-
05-07-2019 - |
Question
If I have a custom UITableViewCell
that doesn't use the textLabel
built in to the cell but instead does its own drawing, how can I change the appearance of the contentView
on selection, like it does automatically for the default text (customizable by setting the selectedTextColor:
)?
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.
Solution
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.
OTHER TIPS
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;
//-------------------------------------------------------------------------
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];
}