当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?


当前回答

你可以使用:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITableView的row at index path方法。

你还可以用:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

在tableview中didselectrowatindexpath方法。

其他回答

swift 3的固定解决方案

cell.selectionStyle = .none

试着输入:

cell.selected = NO;

它将在需要时取消选择您的行。

在Swift3中…

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let r = indexPath.row
    print("clicked .. \(r)")
    tableView.cellForRow(at: indexPath)?.setSelected(false, animated: true)
}

你所要做的就是在UITableViewCell实例上设置选择样式:

objective - c:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3和4.x:

cell.selectionStyle = .none

此外,确保你没有在你的表视图委托中实现-tableView:didSelectRowAtIndexPath:或者显式地排除你想要没有动作的单元格。

更多信息在这里和这里

场景- 1

如果你不想在表视图中选择某些特定的单元格,你可以在cellForRow函数中为这些单元格设置选择样式。

objective - c

cell.selectionStyle = UITableViewCellSelectionStyleNone;

斯威夫特4.2

cell.selectionStyle = .none

场景- 2

禁用整个表视图的选择:

objective - c

self.tableView.allowsSelection = false;

斯威夫特4.2

self.tableView.allowsSelection = false

从ios6.0开始,UITableViewDelegate有tableView:shouldHighlightRowAtIndexPath:。(请参阅iOS文档。)

此方法允许您将特定行标记为不可突出显示(并且隐式地不可选择),而无需更改单元格的选择样式,也无需使用userInteractionEnabled = NO或这里记录的任何其他技术来打乱单元格的事件处理。