6

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

1

10 Answers 10

19
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Implement tableView:willSelectRowAtIndexPath: delegate method of UITableView and return nil.Returning nil from this method tells the table view to not select the row consequently tableView:didSelectRowAtIndexPath: method will not get called.But doing this does not prevent highlighting the row.To disable highlighting effect on row set cell selection style toUITableViewCellSelectionStyleNone as explained in the above answers.

Comments

2

Just set :

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Comments

2
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Comments

1

If you want stop highlighting use the code say: your desired value to be 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 the selection of cell

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

Comments

1

for Swift 3 :

cell.selectionStyle = UITableViewCellSelectionStyle.none 

Comments

0

The accepted answer was not working for me. This problem driving me crazy for awhile because it would look like a "glitch" on any selection when the next view loaded..

My SOLUTION: I had to go to the .xib file of the actual TableViewCell (my custom cell that I built) select the cell and select the CellView. (Make sure you select the Cell, and NOT a subview within it. It will turn Blue like the picture shows). On the left side click "Selection" -> "None". This SHOULD be the exact same as doing it with code, but for some reason it just wasn't working... enter image description here

Comments

0

This is for the case in which you have a custom cell, but I found this question when I was looking for the answer, so I thought I'd leave this 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 UITableViews, you only have to set it once in the custom cell instead of on every UITableView you use :)

Comments

0

for Swift 4

cell.selectionStyle = UITableViewCell.SelectionStyle.none 

Comments

0

you could simply do this for OBJ C:

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

Comments