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.
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:
.
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
Use tableView: willDisplayCell: forRowAtIndexPath: instead of tableView: didSelectRowAtIndexPath: to get rid of the flash that appears first time you touch the cell.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
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.
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;
}
To make certain row unselected you have to make some changes in two methods of UITableView delegate.
In the method below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
after allocation of the cell, write the code below
if(indexPath.row == someCellNumber) cell.selectionStyle =UITableViewCellSelectionStyleNone;
The above code will prevent highlighting the cell, if somehow user tries to selects.
in this delegate method below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == someCellNumber) return;
//for other cells write the code below...
}
if you don't write if(indexPath.row == someCellNumber) return;
row will still be selected and there is a chance of app crash
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.
- (void)setSelectable:(BOOL)enabled {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setUserInteractionEnabled:enabled];
}
- (BOOL)isSelectable {
BOOL disabled = [self selectionStyle]==UITableViewCellSelectionStyleNone &&
[self isUserInteractionEnabled];
return ! disabled;
}