How to make a cell on a UITableView not selectable?
To make a cell completely non-selectable on a per-cell basis two things are required:
1- As others said:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
2- Implement this delegate method as follows:
// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
return nil;
}
return indexPath;
}
To remove the ability of selecting any table cells, or particular table cells based on the row index, use willSelectRowAt
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
return nil
}
To simply remove the UI effect of selecting the element, set the selection style of the UITableViewCell to UITableViewCellSelectionStyleNone
Swift 5:
selectionStyle = .none
From the Storyboard set the following for the Table View:
Selection: No Selection
Show Selection On Touch: False