Then you can change the AccessoryType to be a checkmark, or none, etc. So when you enter didSelectRowAtIndexPath you could deselect the row, and if its not meant to be selected, simply don't check that row.
To completely prevent selection of the UITableViewCell, have your UITableViewDelegate implement tableView:willSelectRowAtIndexPath:. From that method you can return nil if you do not want the row to be selected.
- (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)path
{
// Determine if row is selectable based on the NSIndexPath.
if (rowIsSelectable) {
return path;
}
return nil;
}
This prevents the row from being selected and tableView:didSelectRowAtIndexPath: from being called. Note, however, that this does not prevent the row from being highlighted.
To Prevent Row Highlighting
If you would like to prevent the row from being visually highlighted on touch, you can ensure that the cell's selectionStyle is set to UITableViewCellSelectionStyleNone, or preferably you can have your UITableViewDelegate implement tableView:shouldHighlightRowAtIndexPath: as follows:
- (BOOL)tableView:(UITableView *)tv shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
// Determine if row is selectable based on the NSIndexPath.
return rowIsSelectable;
}
Set the table cell's selectionStyle property to UITableViewCellSelectionStyleNone. That should prevent it from highlighting, and you can also check that property in your tableView:didSelectRowAtIndexPath:.
Had this problem, too, tried everything already mentioned. The final trick, which got rid of the "blue flash" at selecting a table cell was adding this line:
self.myTableView.allowsSelection = NO;
Not sure whether it was this one line or everything combined, but as total grand result I get no more blue selection or even the blue flash. Happy!
Another way is to add a couple category methods to UITableViewCell. I like this better than Sebastians (also good) answer because the way I'm building my table. I thought it might be helpful to someone else.
Use tableView: willDisplayCell: forRowAtIndexPath: instead of tableView: didSelectRowAtIndexPath: to get rid of the flash that appears first time you touch the cell.
Swift 4:
You can prevent selection and highlighting by using the UITableViewDelegate: shouldHighlightRowAt
This answer assumes you have a custom cell created named: CustomTableViewCell And that you created a boolean inside that custom cell named: isSelectable