I have a cell that I am inserting to the top of a UITableView. How can I make sure that when the user clicks on the cell, it doesn't show the blue selected indicator?
15 Answers
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 3 Comments
tableView:didSelectRowAtIndexPath: still gets called when the cell is tapped. To actually prevent selection you need to implement tableView:willSelectRowAtIndexPath: on your UITableViewDelegate and return nil for non-selectable rows.cell.selectionStyle = UITableViewCellSelectionStyle.nonecell.selectionStyle = .noneTo 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; } Comments
you can do
cell.selectionStyle = UITableViewCellSelectionStyleNone; 1 Comment
for Swift 3 you can use
cell.isUserInteractionEnabled = false 5 Comments
tableView:didSelectRowAtIndexPath delegate method, detect cell index and just do nothing and return.The question was how to make certain cells selectable and others not. This was my solution, (after trying lots of other suggestions):
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if (indexPath.row == 1 || indexPath.row == 2) { return indexPath } return nil } Comments
For Swift 3:
cell.selectionStyle = .none 2 Comments
Implement this method of UITableViewDelegate
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { return NO; } Comments
To remove the ability of selecting any table cells, use willSelectRowAt
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return nil } or by definition during table initialization (if you are not in table editing mode):
tableView.allowsSelection = false if you need to restrict any table cells selection during table editing:
tableView.allowsSelectionDuringEditing = false To remove the ability of selecting the top cell use the same function but with statement:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if indexPath.row == 0 { return nil } return indexPath } To simply remove the UI effect of selecting the element, set the selection style of the UITableViewCell to UITableViewCellSelectionStyleNone
Swift 5:
selectionStyle = .none 
