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 Answers
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.
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.
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];
}
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...
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 UITableView
s, you only have to set it once in the custom cell instead of on every UITableView
you use :)