Custom UITableViewCell selection style?

SimplyKiwi picture SimplyKiwi · Aug 12, 2012 · Viewed 96.1k times · Source

When I click on my UITableViewCell, the background part (the areas that my background image doesn't cover) turns blue when I click on the cell. Also, all of the UILabels on the cell turn to white when it is clicked which is what I want.

However what I do not want is the blue background when I click it but if I do selectionstylenone, then I lose the highlighted colours for the UILabels in the cell.

So is there any way to just get rid of the blue background when the cell is clicked but to keep the highlighted colors of the UILabels?

Answer

jonkroll picture jonkroll · Aug 12, 2012

You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell and override the default implementation of setHighlighted:animated with your own implementation that sets the label colors to however you want depending on the highlighted state.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}