[ios] How can I disable the UITableView selection?

When you tap a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?

This question is related to ios uitableview cocoa-touch

The answer is


You can use selectionStyle property of UITableViewCell

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

Or

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Also, do not implement below delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }

If you have created Xib/Storyboard file then you can change setUserInteractionEnabled property of tableview to No by unchecking it. This will make your tableview to Read-Only.


You can use :

cell.selectionStyle = UITableViewCellSelectionStyleNone;

in the cell for row at index path method of your UITableView.

Also you can use :

[tableView deselectRowAtIndexPath:indexPath animated:NO];

in the tableview didselectrowatindexpath method.


As of iOS 6.0, UITableViewDelegate has tableView:shouldHighlightRowAtIndexPath:. (Read about it in the iOS Documentation.)

This method lets you mark specific rows as unhighlightable (and implicitly, unselectable) without having to change a cell's selection style, messing with the cell's event handling with userInteractionEnabled = NO, or any other techniques documented here.


If you want selection to only flash, not remain in the selected state, you can call, in

didSelectRowAtIndexPath

the following

[tableView deselectRowAtIndexPath:indexPath animated:YES];

so it will flash the selected state and revert.


At least as of iOS 6, you can override methods in your custom cell to prevent the blue highlight. No other interaction is disabled or affected. All three must be overridden.

- (void) setHighlighted:(BOOL)highlighted
{
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated
{
}

You can use ....

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

While this is the best and easiest solution to prevent a row from showing the highlight during selection

cell.selectionStyle = UITableViewCellSelectionStyleNone;

I'd like to also suggest that it's occasionally useful to briefly show that the row has been selected and then turning it off. This alerts the users with a confirmation of what they intended to select:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}

From the UITableViewDelegate Protocol you can use the method willSelectRowAtIndexPath and return nil if you don't want the row selected.

In the same way the you can use the willDeselectRowAtIndexPath method and return nil if you don't want the row to deselect.


From UITableViewDataSource Protocol, inside method cellForRowAt add:

let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)                
cell.selectionStyle = .none
return cell

OR

You can goto Storyboard > Select Cell > Identity Inspector > Selection and select none from dropdown.


Try to type:

cell.selected = NO;

It will deselect your row when needed.

In Swift3 ...

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let r = indexPath.row
    print("clicked .. \(r)")
    tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}

You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property:(While tapping the cell).

cell.selectionStyle = UITableViewCellSelectionStyle.None

Objective-C:

  1. Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though.

    self.tableView.allowsSelection = NO;
    
  2. Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  3. Below snippet disable everything on the cell. This will disable the interaction to buttons, textfields:

    self.tableView.userInteractionEnabled = false;
    

Swift:

Below are the Swift equivalent of above Objective-C solutions:

  1. Replacement of First Solution

    self.tableView.allowsSelection = false
    
  2. Replacement of Second Solution

    cell?.selectionStyle = UITableViewCellSelectionStyle.None
    
  3. Replacement of Third Solution

    self.tableView.userInteractionEnabled = false
    

I've been battling with this quite profusely too, having a control in my UITableViewCell prohibited the use of userInteractionEnabled property. I have a 3 cell static table for settings, 2 with dates, 1 with an on/off switch. After playing about in Storyboard/IB i've managed to make the bottom one non-selectable, but when you tap it the selection from one of the top rows disappears. Here is a WIP image of my settings UITableView:

Settings UITableView

If you tap the 3rd row nothing at all happens, the selection will stay on the second row. The functionality is practically a copy of Apple's Calendar app's add event time selection screen.

The code is surprisingly compatible, all the way down to IOS2 =/:

- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return nil;
    }
    return indexPath;
}

This works in conjunction with setting the selection style to none, so the cell doesn't flicker on touch down events


All you have to do is set the selection style on the UITableViewCell instance using either:

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3 and 4.x:

cell.selectionStyle = .none

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

More info here and here


cell.selectionStyle = UITableViewCellSelectionStyleNone;

We can write code like

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

but when we have custom cell xib above line give warning at that time for

custom cell xib

we need to set selection style None from the interface builder


To sum up what I believe are the correct answers based on my own experience in implementing this:

If you want to disable selection for just some of the cells, use:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)

If you have buttons in your cells that need to be clicked, you need to instead:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you also need to ignore any clicks on the cell in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.

If you want to disable selection for the whole table, use:

tableView.allowsSelection = NO;

(Credit to Paulo De Barros, thanks!)


To disable the highlighting of the UItableviewcell

cell.selectionStyle = UITableViewCellSelectionStyleNone;

And should not allow the user to interact with the cell.

cell.userInteractionEnabled = NO;

You can disable table cell highight using below code in (iOS) Xcode 9 , Swift 4.0

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


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

Very simple stuff. Before returning the tableview Cell use the style property of the table view cell.

Just write this line of code before returning table view cell
cell.selectionStyle = .none


The better approach will be:

cell.userInteractionEnabled = NO;

This approach will not call didSelectRowAtIndexPath: method.


Objective-C:

  1. Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though.

    self.tableView.allowsSelection = NO;
    
  2. Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  3. Below snippet disable everything on the cell. This will disable the interaction to buttons, textfields:

    self.tableView.userInteractionEnabled = false;
    

Swift:

Below are the Swift equivalent of above Objective-C solutions:

  1. Replacement of First Solution

    self.tableView.allowsSelection = false
    
  2. Replacement of Second Solution

    cell?.selectionStyle = UITableViewCellSelectionStyle.None
    
  3. Replacement of Third Solution

    self.tableView.userInteractionEnabled = false
    

FIXED SOLUTION FOR SWIFT 3

cell.selectionStyle = .none

The best solution would be Making The selection Style None

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

However, Here we are considering the fact that there are no custom images used for selected state.


The better approach will be:

cell.userInteractionEnabled = NO;

This approach will not call didSelectRowAtIndexPath: method.


In case anyone needs answer for Swift:

cell.selectionStyle = .None

Scenario - 1

If you don't want selection for some specific cells on the tableview, you can set selection style in cellForRow function for those cells.

Objective-C

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Swift 4.2

cell.selectionStyle = .none

Scenario - 2

For disabling selection on the whole table view :

Objective-C

self.tableView.allowsSelection = false;

Swift 4.2

self.tableView.allowsSelection = false

To sum up what I believe are the correct answers based on my own experience in implementing this:

If you want to disable selection for just some of the cells, use:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)

If you have buttons in your cells that need to be clicked, you need to instead:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you also need to ignore any clicks on the cell in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.

If you want to disable selection for the whole table, use:

tableView.allowsSelection = NO;

(Credit to Paulo De Barros, thanks!)


All you have to do is set the selection style on the UITableViewCell instance using either:

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3 and 4.x:

cell.selectionStyle = .none

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

More info here and here


UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

Happy coding !!!


try this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

and

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you can also set selection style using interfacebuilder.


In your UITableViewCell's XIB in Attribute Inspector set value of Selection to None.

enter image description here


The best solution would be Making The selection Style None

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

However, Here we are considering the fact that there are no custom images used for selected state.


You can use ....

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

You can disable table cell highight using below code in (iOS) Xcode 9 , Swift 4.0

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


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

In your UITableViewCell's XIB in Attribute Inspector set value of Selection to None.

enter image description here


As of iOS 6.0, UITableViewDelegate has tableView:shouldHighlightRowAtIndexPath:. (Read about it in the iOS Documentation.)

This method lets you mark specific rows as unhighlightable (and implicitly, unselectable) without having to change a cell's selection style, messing with the cell's event handling with userInteractionEnabled = NO, or any other techniques documented here.


I've been battling with this quite profusely too, having a control in my UITableViewCell prohibited the use of userInteractionEnabled property. I have a 3 cell static table for settings, 2 with dates, 1 with an on/off switch. After playing about in Storyboard/IB i've managed to make the bottom one non-selectable, but when you tap it the selection from one of the top rows disappears. Here is a WIP image of my settings UITableView:

Settings UITableView

If you tap the 3rd row nothing at all happens, the selection will stay on the second row. The functionality is practically a copy of Apple's Calendar app's add event time selection screen.

The code is surprisingly compatible, all the way down to IOS2 =/:

- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return nil;
    }
    return indexPath;
}

This works in conjunction with setting the selection style to none, so the cell doesn't flicker on touch down events


You can also disable selection of row from interface builder itself by choosing NoSelection from the selection option(of UITableView Properties) in inspector pane as shown in the below image

UITableView Inspector


In case anyone needs answer for Swift:

cell.selectionStyle = .None

Try to type:

cell.selected = NO;

It will deselect your row when needed.

In Swift3 ...

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let r = indexPath.row
    print("clicked .. \(r)")
    tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}

FIXED SOLUTION FOR SWIFT 3

cell.selectionStyle = .none

To disable the highlighting of the UItableviewcell

cell.selectionStyle = UITableViewCellSelectionStyleNone;

And should not allow the user to interact with the cell.

cell.userInteractionEnabled = NO;

You can use :

cell.selectionStyle = UITableViewCellSelectionStyleNone;

in the cell for row at index path method of your UITableView.

Also you can use :

[tableView deselectRowAtIndexPath:indexPath animated:NO];

in the tableview didselectrowatindexpath method.


You can use selectionStyle property of UITableViewCell

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

Or

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Also, do not implement below delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }

If you have created Xib/Storyboard file then you can change setUserInteractionEnabled property of tableview to No by unchecking it. This will make your tableview to Read-Only.


At least as of iOS 6, you can override methods in your custom cell to prevent the blue highlight. No other interaction is disabled or affected. All three must be overridden.

- (void) setHighlighted:(BOOL)highlighted
{
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated
{
}

1- All you have to do is set the selection style on the UITableViewCell instance using either:


Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None


Swift 3:

cell.selectionStyle = .none


2 - Don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

3 - Further,You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.


4 - You can disable table cell highlight using below code in (iOS) Xcode 9 , Swift 4.0

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


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

All you have to do is set the selection style on the UITableViewCell instance using either:

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3 and 4.x:

cell.selectionStyle = .none

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

More info here and here


Disable selection for all UITableViewCells in the UITableView

tableView.allowsSelection = false

Disable selection for specific UITableViewCells

cell.selectionStyle = UITableViewCell.SelectionStyle.none

Scenario - 1

If you don't want selection for some specific cells on the tableview, you can set selection style in cellForRow function for those cells.

Objective-C

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Swift 4.2

cell.selectionStyle = .none

Scenario - 2

For disabling selection on the whole table view :

Objective-C

self.tableView.allowsSelection = false;

Swift 4.2

self.tableView.allowsSelection = false

Directly disable highlighting of TableViewCell into storyboard

enter image description here


cell.selectionStyle = UITableViewCellSelectionStyleNone;

This is what I use ,in cellForRowAtIndexPath write this code.:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

If you want selection to only flash, not remain in the selected state, you can call, in

didSelectRowAtIndexPath

the following

[tableView deselectRowAtIndexPath:indexPath animated:YES];

so it will flash the selected state and revert.


Because I've read this post recently and it has helped me, I wanted to post another answer to consolidate all of the answers (for posterity).



So, there are actually 5 different answers depending on your desired logic and/or result:

1.To disable the blue highlighting without changing any other interaction of the cell:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.

NOTE: As Tony Million noted above, this does NOT prevent tableView:didSelectRowAtIndexPath:. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section.

Another way I thought of to test for the tapping of a cell like this is:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // A case was selected, so push into the CaseDetailViewController
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        // Handle tap code here
    }
}



2.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:

[tableView setAllowsSelection:NO];

In my testing, this still allows controls inside the UITableViewCell to be interactive.


3.To make a cell "read-only", you can simply do this:

[cell setUserInteractionEnabled:NO];



4.To make an entire table "read-only"

[tableView setUserInteractionEnabled:NO];



5.To determine on-the-fly whether to highlight a cell (which according to this answer implicitly includes selection), you can implement the following UITableViewDelegate protocol method:

- (BOOL)tableView:(UITableView *)tableView 
   shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

1- All you have to do is set the selection style on the UITableViewCell instance using either:


Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None


Swift 3:

cell.selectionStyle = .none


2 - Don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

3 - Further,You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.


4 - You can disable table cell highlight using below code in (iOS) Xcode 9 , Swift 4.0

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


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

While this is the best and easiest solution to prevent a row from showing the highlight during selection

cell.selectionStyle = UITableViewCellSelectionStyleNone;

I'd like to also suggest that it's occasionally useful to briefly show that the row has been selected and then turning it off. This alerts the users with a confirmation of what they intended to select:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}

You Can also set the background color to Clear to achieve the same effect as UITableViewCellSelectionStyleNone, in case you don't want to/ can't use UITableViewCellSelectionStyleNone.

You would use code like the following:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

This may degrade your performance as your adding an extra colored view to each cell.


Swift 3,4 and 5

Better practice, write code in UITableViewCell

For example, you have UITableViewCell with the name MyCell, In awakeFromNib just write self.selectionStyle = .none

Full example:

class MyCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
}

This is what I use ,in cellForRowAtIndexPath write this code.:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

You can use this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.


You Can also set the background color to Clear to achieve the same effect as UITableViewCellSelectionStyleNone, in case you don't want to/ can't use UITableViewCellSelectionStyleNone.

You would use code like the following:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

This may degrade your performance as your adding an extra colored view to each cell.


Swift 3,4 and 5

Better practice, write code in UITableViewCell

For example, you have UITableViewCell with the name MyCell, In awakeFromNib just write self.selectionStyle = .none

Full example:

class MyCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
    }
    
}

From UITableViewDataSource Protocol, inside method cellForRowAt add:

let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)                
cell.selectionStyle = .none
return cell

OR

You can goto Storyboard > Select Cell > Identity Inspector > Selection and select none from dropdown.


Swift Solution w/ Custom Cell:

import Foundation

class CustomTableViewCell: UITableViewCell
{
  required init(coder aDecoder: NSCoder)
  {
    fatalError("init(coder:) has not been implemented")
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.selectionStyle = UITableViewCellSelectionStyle.None
  } 
}

I am using this, which works for me.

cell?.selectionStyle = UITableViewCellSelectionStyle.None

Because I've read this post recently and it has helped me, I wanted to post another answer to consolidate all of the answers (for posterity).



So, there are actually 5 different answers depending on your desired logic and/or result:

1.To disable the blue highlighting without changing any other interaction of the cell:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.

NOTE: As Tony Million noted above, this does NOT prevent tableView:didSelectRowAtIndexPath:. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section.

Another way I thought of to test for the tapping of a cell like this is:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // A case was selected, so push into the CaseDetailViewController
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        // Handle tap code here
    }
}



2.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:

[tableView setAllowsSelection:NO];

In my testing, this still allows controls inside the UITableViewCell to be interactive.


3.To make a cell "read-only", you can simply do this:

[cell setUserInteractionEnabled:NO];



4.To make an entire table "read-only"

[tableView setUserInteractionEnabled:NO];



5.To determine on-the-fly whether to highlight a cell (which according to this answer implicitly includes selection), you can implement the following UITableViewDelegate protocol method:

- (BOOL)tableView:(UITableView *)tableView 
   shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

I am using this, which works for me.

cell?.selectionStyle = UITableViewCellSelectionStyle.None

Disable selection for all UITableViewCells in the UITableView

tableView.allowsSelection = false

Disable selection for specific UITableViewCells

cell.selectionStyle = UITableViewCell.SelectionStyle.none

You can also do it from the storyboard. Click the table view cell and in the attributes inspector under Table View Cell, change the drop down next to Selection to None.


You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property:(While tapping the cell).

cell.selectionStyle = UITableViewCellSelectionStyle.None

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

Happy coding !!!


You can also disable selection of row from interface builder itself by choosing NoSelection from the selection option(of UITableView Properties) in inspector pane as shown in the below image

UITableView Inspector


You can use this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

try this

cell.selectionStyle = UITableViewCellSelectionStyleNone;

and

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you can also set selection style using interfacebuilder.


Swift Solution w/ Custom Cell:

import Foundation

class CustomTableViewCell: UITableViewCell
{
  required init(coder aDecoder: NSCoder)
  {
    fatalError("init(coder:) has not been implemented")
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.selectionStyle = UITableViewCellSelectionStyle.None
  } 
}

All you have to do is set the selection style on the UITableViewCell instance using either:

Objective-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3 and 4.x:

cell.selectionStyle = .none

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

More info here and here


Directly disable highlighting of TableViewCell into storyboard

enter image description here


From the UITableViewDelegate Protocol you can use the method willSelectRowAtIndexPath and return nil if you don't want the row selected.

In the same way the you can use the willDeselectRowAtIndexPath method and return nil if you don't want the row to deselect.


Very simple stuff. Before returning the tableview Cell use the style property of the table view cell.

Just write this line of code before returning table view cell
cell.selectionStyle = .none


Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to uitableview

Adding a UISegmentedControl to UITableView make UITableViewCell selectable only while editing How to fix Error: this class is not key value coding-compliant for the key tableView.' UITableView example for Swift Swift - how to make custom header for UITableView? How to insert new cell into UITableView in Swift How to detect tableView cell touched or clicked in swift Dynamic Height Issue for UITableView Cells (Swift) UIButton action in table view cell How to get the indexpath.row when an element is activated?

Examples related to cocoa-touch

Include of non-modular header inside framework module Move textfield when keyboard appears swift Create space at the beginning of a UITextField Navigation bar with UIImage for title Generate a UUID on iOS from Swift How do I write a custom init for a UIView subclass in Swift? creating custom tableview cells in swift How would I create a UIAlertView in Swift? Get current NSDate in timestamp format How do you add an in-app purchase to an iOS application?