ios - How can I disable the UITableView selection?

ID : 1018

viewed : 197

Tags : iosuitableviewcocoa-touchios

Top 5 Answer for ios - How can I disable the UITableView selection?

vote vote


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


cell.selectionStyle = UITableViewCellSelectionStyleNone; 


[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

vote vote


For me, the following worked fine:

tableView.allowsSelection = false 

This means didSelectRowAt# simply won't work. That is to say, touching a row of the table, as such, will do absolutely nothing. (And hence, obviously, there will never be a selected-animation.)

(Note that if, on the cells, you have UIButton or any other controls, of course those controls will still work. Any controls you happen to have on the table cell, are totally unrelated to UITableView's ability to allow you to "select a row" using didSelectRowAt#.)

Another point to note is that: This doesn't work when the UITableView is in editing mode. To restrict cell selection in editing mode use the code as below:

tableView.allowsSelectionDuringEditing = false  
vote vote


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


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!)

vote vote


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.

Top 3 video Explaining ios - How can I disable the UITableView selection?