Sheehan Alam
Sheehan Alam

Reputation: 60919

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?

Upvotes: 78

Views: 45157

Answers (15)

nothing-to-add
nothing-to-add

Reputation: 1

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

Upvotes: 0

John Wang
John Wang

Reputation: 6126

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

Upvotes: 87

Allister Smith
Allister Smith

Reputation: 71

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        
}

Upvotes: 5

HanKBeatz
HanKBeatz

Reputation: 162

Swift 5

you may wanna try this too

tableView.allowsSelection = false

Upvotes: 1

Enamul Haque
Enamul Haque

Reputation: 5063

Swift 5

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
    cell.selectionStyle = .none
}

Upvotes: 1

malhal
malhal

Reputation: 30754

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

Upvotes: 65

Shiyan Xu
Shiyan Xu

Reputation: 1192

Implement this method of UITableViewDelegate

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

Upvotes: 1

user8043247
user8043247

Reputation:

for Swift 3 you can use

 cell.isUserInteractionEnabled = false

Upvotes: 6

David Villegas
David Villegas

Reputation: 498

For Swift 3:

cell.selectionStyle = .none

Upvotes: 1

Ben
Ben

Reputation: 984

Update in Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none

Upvotes: 2

mythicalcoder
mythicalcoder

Reputation: 3301

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!

Upvotes: 5

Mitch Dalton
Mitch Dalton

Reputation: 31

myTable.allowsSelection = false

Upvotes: 3

Eduardo Irias
Eduardo Irias

Reputation: 1053

From the Storyboard set the following for the Table View:

Selection: No Selection

Show Selection On Touch: False

enter image description here

Upvotes: 16

Dustin Williams
Dustin Williams

Reputation: 3913

Swift Syntax:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Upvotes: 5

Anju
Anju

Reputation: 524

you can do

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Upvotes: 13

Related Questions