6
votes

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?

10

10 Answers

18
votes
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.

4
votes

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.

2
votes

Just set :

cell.selectionStyle = UITableViewCellSelectionStyleNone;
2
votes
cell.selectionStyle = UITableViewCellSelectionStyleNone;
1
votes

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];

}
1
votes

for Swift 3 :

cell.selectionStyle = UITableViewCellSelectionStyle.none
0
votes

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

0
votes

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

0
votes

for Swift 4

cell.selectionStyle = UITableViewCell.SelectionStyle.none
0
votes

you could simply do this for OBJ C:

 cell.selectionStyle = UITableViewCellSelectionStyleNone;