How to make a cell on a UITableView not selectable?

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?


Solution 1:

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

Solution 2:

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;
}

Solution 3:

From the Storyboard set the following for the Table View:

Selection: No Selection

Show Selection On Touch: False

enter image description here