How to make a cell on a UITableView not selectable?
Asked Answered
S

15

78

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?

San answered 14/4, 2010 at 22:34 Comment(0)
R
87

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
Rosaliarosalie answered 14/4, 2010 at 22:39 Comment(3)
This doesn't make the cell non-selectable it just means the visual appearance of the cell doesn't change when it is selected. 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.Bassesalpes
Swift 3 - cell.selectionStyle = UITableViewCellSelectionStyle.noneHumorist
Or simply: cell.selectionStyle = .noneRadtke
T
65

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;
}
Twinscrew answered 12/7, 2013 at 3:52 Comment(0)
D
16

From the Storyboard set the following for the Table View:

Selection: No Selection

Show Selection On Touch: False

enter image description here

Diploblastic answered 23/9, 2015 at 18:37 Comment(0)
A
13

you can do

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Anurous answered 26/4, 2012 at 11:35 Comment(1)
One dot is missing. Here is the fixed version: cell.selectionStyle = UITableViewCellSelectionStyle.NoneDrill
T
6

for Swift 3 you can use

 cell.isUserInteractionEnabled = false
Tudela answered 13/9, 2017 at 5:21 Comment(5)
just past that code into cellForRowAt indexPath method of table view , its simpleTudela
this method is useful when u want to user interaction disable on table view otherwise you can use method share by Mr Ben BelowTudela
Works in Swift 4 also.Helices
It is working fine for me. Should be the correct answer. Another way is that in tableView:didSelectRowAtIndexPath delegate method, detect cell index and just do nothing and return.Kennithkennon
My favorite part about this answer is that you can declare it INSIDE the cell. If you have a cell (like an empty state cell) that you never want to respond to taps, this is a good way to go.Sorrows
P
5

Swift Syntax:

cell.selectionStyle = UITableViewCellSelectionStyle.None
Pentameter answered 18/3, 2015 at 21:16 Comment(0)
S
5

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!

Synchromesh answered 11/2, 2017 at 6:34 Comment(0)
M
5

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        
}
Miyamoto answered 30/1, 2020 at 14:27 Comment(0)
A
3
myTable.allowsSelection = false
Alpha answered 24/4, 2016 at 17:39 Comment(0)
E
2

Update in Swift 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none
Espy answered 4/5, 2017 at 22:58 Comment(0)
I
1

Implement this method of UITableViewDelegate

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return NO;
}
Inspan answered 20/11, 2015 at 16:23 Comment(0)
C
1

For Swift 3:

cell.selectionStyle = .none
Corrosion answered 22/7, 2017 at 2:42 Comment(2)
As suggested Above "This doesn't make the cell non-selectable it just means the visual appearance of the cell doesn't change when it is selected. 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. –"Rinker
@Rinker as indicated in the original question, this person only wants to hide "the blue selected indicator". That is not the same as wanting to make it completely non-selectable. Simply put the person asking the question is only trying to hide visual indication of a selection.Corrosion
Y
1

Swift 5

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell;
    cell.selectionStyle = .none
}
Yoruba answered 3/1, 2020 at 6:13 Comment(0)
V
1

Swift 5

you may wanna try this too

tableView.allowsSelection = false
Vancouver answered 15/1, 2020 at 5:31 Comment(0)
N
0

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
Notum answered 15/11, 2023 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.