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 20,000+ Swift developers and enthusiasts who get my weekly updates.