How to make a cell on a UITableView not selectable?

IphoneObjective CCocoa TouchUitableview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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 - Iphone

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 - Iphone

From the Storyboard set the following for the Table View:

Selection: No Selection

Show Selection On Touch: False

enter image description here

Solution 4 - Iphone

you can do

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Solution 5 - Iphone

for Swift 3 you can use

 cell.isUserInteractionEnabled = false

Solution 6 - Iphone

Swift Syntax:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Solution 7 - Iphone

I am answering from point of view of disabling TableViewCell. You can use the storyboard.

XCode Version 8.2.1 (8C1002)

Select the TableVewCell on storyboard and following will be visible on the right side panel - Utilities.

Utilities Sidebar

Make the Selection: None

That's all!

Solution 8 - Iphone

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        
}

Solution 9 - Iphone

myTable.allowsSelection = false

Solution 10 - Iphone

Update in Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

Solution 11 - Iphone

Implement this method of UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return NO;
}

Solution 12 - Iphone

For Swift 3:

cell.selectionStyle = .none

Solution 13 - Iphone

Swift 5

you may wanna try this too

tableView.allowsSelection = false

Solution 14 - Iphone

Swift 5

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
    cell.selectionStyle = .none
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheehan AlamView Question on Stackoverflow
Solution 1 - IphoneJohn WangView Answer on Stackoverflow
Solution 2 - IphonemalhalView Answer on Stackoverflow
Solution 3 - IphoneEduardo IriasView Answer on Stackoverflow
Solution 4 - IphoneAnjuView Answer on Stackoverflow
Solution 5 - Iphoneuser8043247View Answer on Stackoverflow
Solution 6 - IphoneDustin WilliamsView Answer on Stackoverflow
Solution 7 - IphonemythicalcoderView Answer on Stackoverflow
Solution 8 - IphoneAllister SmithView Answer on Stackoverflow
Solution 9 - IphoneMitch DaltonView Answer on Stackoverflow
Solution 10 - IphoneBenView Answer on Stackoverflow
Solution 11 - IphoneRaymondView Answer on Stackoverflow
Solution 12 - IphoneDavid VillegasView Answer on Stackoverflow
Solution 13 - IphoneHanKBeatzView Answer on Stackoverflow
Solution 14 - IphoneEnamul HaqueView Answer on Stackoverflow