selection color for a UITableViewCell

12,703

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];
}
Share:
12,703
Sophie Alpert
Author by

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, 2022

Comments

  • Sophie Alpert
    Sophie Alpert almost 2 years

    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.

  • Sophie Alpert
    Sophie Alpert almost 15 years
    That'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
    Thomas almost 15 years
    When you talked about "own drawing" I immediately thought subclassing. My bad. Glad you find a solution