All you have to do is set the selection style on the UITableViewCell
instance using either:
Objective-C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Swift 2:
cell.selectionStyle = UITableViewCellSelectionStyle.None
Swift 3 and 4.x:
cell.selectionStyle = .none
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath:
in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
More info here and here
Here is the most efficient way I have come across to solve this problem, use the willDisplayCell delegate method (this takes care of the white color for the text label background as well when using cell.textLabel.text and/or cell.detailTextLabel.text):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
When this delegate method is called the color of the cell is controlled via the cell rather than the table view, as when you use:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
So within the body of the cell delegate method add the following code to alternate colors of cells or just use the function call to make all the cells of the table the same color.
if (indexPath.row % 2)
{
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];
This solution worked well in my circumstance...
Best Answer
It isn't easy unfortunately, but you don't have to resort to images. Here is a class I modified - it came from How to customize the background/border colors of a grouped table view cell?, but I added the ability to switch between a solid fill color or a gradient, as well as a property to use the blue selection mode so you can use this as the cell selectedBackgroundView if you support selection.
Modes: 1.useBlackGradient or useWhiteGradient to use built-in gradients 2.gradientStartColor/gradientEndColor properties to setup your own 3.useBlueSelectionGradient = YES to put it into selection mode using the blue highlight (same as apple uses)
Here is how you'd use it:
Here is the header and implementation: