views:

3278

answers:

6

How can I set the UITableView's cell property to be unselectable? I don't want to see that blue selection box when the user taps on the cell.

A: 

Apple says that the first thing you should do in didSelectRowAtIndexPath is to deselect the row

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

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.

Table View Programming Guide

ryanday
In that case, when a user touches the cell, it will be highlighted for a short time and then unhighlighted - but as I understand, the point is to completely stop it from highlighting.
Psionides
+22  A: 

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;
}
Sebastian Celis
+11  A: 

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:.

Daniel Dickison
That does work in that the user will not see a selection, but didSelectRowAtIndexPath will still be called (where you could ignore it)
Kendall Helmstetter Gelner
Kendall is right. Follow Sebastian Celis' answer to prevent didSelectRowAtIndexPath from being called in the first place. You should also set selectionStyle, though, to prevent the highlighting.
Daniel Dickison
A: 

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!

JOM
Problem with this is that it makes it so all cells in the tableview are not selectable. If you want some cell to be selectable, and others to not be selectable, this won't do it. Sebastian's approach worked for me.
Terry
True, allowsSelection = NO will disable selection for all cells.
JOM
Is there an equivalent property in Interface Builder for this property?
chrish
A: 

use this:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

hanumanDev