How To Set A Custom UITableViewCell Selection Style Color

The storyboard only gives three options for the Selection color of a UITableViewCell:

Screen Shot 2013-07-09 at 6.56.42 AM

However, for my ShopLater app, I wanted the selected cell to be pink! Why only blue or gray?!!! I was surprised when I found the answer on how to do it, but it makes sense. Basically the the selection color of a UITableViewCell is actually a background color on a subview of the cell.

So, to change the selection color, simply assign a different selection view to the selected background view of your cell in your code!

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sMenuStoreCell 
                                                            forIndexPath:indexPath];

   // configure your cell here

    // this is where you set your color view
    UIView *customColorView = [[UIView alloc] init];
    customColorView.backgroundColor = [UIColor colorWithRed:180/255.0 
                                                      green:138/255.0 
                                                       blue:171/255.0 
                                                      alpha:0.5];
    cell.selectedBackgroundView =  customColorView;

    return cell;
}

Here is what the ShopLater selected cell looks like with this code in place:

shoplater_selection

This is really cool. Just think of the possibilities. You could actually configure a fun background image instead of just a color when the user selects a cell! You can also have different selection backgrounds for each cell. I just hope Apple would approve the fun 🙂

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • jjthetrader

    Thank you so much for this!

    In swift I did this:
    . var myCustomSelectionColorView = UIView()
    myCustomSelectionColorView.backgroundColor = UIColor.greenColor()
    cell.selectedBackgroundView = myCustomSelectionColorView

    • MizAkita

      thanks for this!

  • RemyDCF

    Thank You

  • thanks for this. The advantage of having an experience working with Objective-C is really helpful, I started my journey in iOS using that language, and now I’m using Swift in my new company. 😀 Just saying that I can easily convert this to Swift. haha.

    DR