Is it possible to disable UITableView selection of a single cell?

When you click on a row in a UITableView , the row is highlighted and selected. Is it possible to disable this, so pressing a line does nothing?

+5
source share
9 answers
 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Also, make sure that you either do not implement -tableView:didSelectRowAtIndexPath: in the table view delegate , or explicitly exclude cells in which you want to not perform any actions if you implement it.

+18
source

tableView:willSelectRowAtIndexPath: delegate method to UITableView and return nil . Returning nil from this method says that the table view does not select a row, therefore the tableView:didSelectRowAtIndexPath: method will not be called. But this does not prevent row.To highlighting to disable the selection effect on the rowset cell selection style until UITableViewCellSelectionStyleNone , as described in the answers above.

+4
source

Just install:

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+2
source
 cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+1
source

If you want to stop the selection, use the code: your desired value is 19

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexpath.row == '19') cell.selectionStyle = UITableViewCellSelectionStyleNone; else cell.selectionStyle = UITableViewCellSelectionStyleGray; } 

But, if you want to control cell selection

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexpath.row == '19') [tableView deselectRowAtIndexPath:indexPath animated:NO]; } 
+1
source

for Swift 3:

 cell.selectionStyle = UITableViewCellSelectionStyle.none 
+1
source

The accepted answer did not work for me. This problem drives me crazy for a while, because it will look like a β€œcrash” in any selection when the next view loads.

My solution: I needed to go to the .xib file of the actual TableViewCell (my own cell that I built), select the cell and select CellView. (Make sure you select the Cell, NOT the subview inside it. It will turn blue, as shown in the figure). On the left side, click Select β†’ None. This MUST be the same as with the code, but for some reason it just doesn't work ... enter image description here

0
source

This is for the case when you have a custom cell , but I found this question when I was looking for an answer, so I decided to leave it here in case this helps others.


Go to your custom cell and set selectionStyle to .none in the awakeFromNib method:

 override func awakeFromNib() { super.awakeFromNib() // disable selection selectionStyle = .none; } 

If you use this custom cell in multiple UITableView , you will need to set it only once in the custom cell, and not in every UITableView you use :)

0
source

for Swift 4

 cell.selectionStyle = UITableViewCell.SelectionStyle.none 
0
source

All Articles