Monday, February 1, 2010

How to Disable UITableCell Selection

Sometimes you just want to show a list (UITableView) without selecting any row. By default a table (cell) is selectable, so how to disable it. What worked for me was defining each cell separately as non-selectable:

cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

You could also try controlling the whole table, but it didn't work for me:

- (void)viewDidLoad
{
// Prevent message cell selection
self.chatTableView.allowsSelection = NO;
// HOX: not iPhone 2.2.1 compatible --> crash
[super viewDidLoad];
}

This is also supposed to work, but I recall I still got a selection flash:


- (NSIndexPath *) tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)_indexPath
{
// Return nil if you don't want the row selected.
// Hox: interferes with didSelectRowAtIndexPath
return nil;
}

2 comments:

  1. Hi,
    Thanks for the tips, your first solution works fine for me as i only need to remove selection on some cell not on all the table.

    cesar

    ReplyDelete
  2. If you want to disable cell selection for the entire table, simply use

    [tableView setAllowsSelection:NO];

    ReplyDelete