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


当前回答

对我来说,以下方法效果不错:

tableView.allowsSelection = false

这意味着didselectrow#将无法工作。也就是说,触摸表中的一行,本身不会有任何作用。(因此,很明显,永远不会有选中动画。)

(请注意,如果在单元格上有UIButton或任何其他控件,当然这些控件仍然可以工作。你碰巧在表格单元格上的任何控件,都与UITableView允许你使用didSelectRowAt#“选择一行”的能力完全无关。)

另一点需要注意的是:当UITableView处于编辑模式时,这是无效的。要在编辑模式下限制单元格选择,请使用如下代码:

tableView.allowsSelectionDuringEditing = false 

其他回答

在UITableViewDataSource协议中,在方法cellForRowAt中添加:

let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)                
cell.selectionStyle = .none
return cell

OR

你可以到故事板>选择单元格>身份检查器>选择,从下拉菜单中选择none。

你只需要把这段代码放到cellForRowAtIndexPath中

禁用单元格的选择属性:(在点击单元格时)。

cell.selectionStyle = UITableViewCellSelectionStyle.None

你可以使用:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITableView的row at index path方法。

你还可以用:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

在tableview中didselectrowatindexpath方法。

objective - c:

Below snippet disable highlighting but it also disable the call to didSelectRowAtIndexPath. So if you are not implementing didSelectRowAtIndexPath then use below method. This should be added when you are creating the table. This will work on buttons and UITextField inside the cell though. self.tableView.allowsSelection = NO; Below snippet disable highlighting and it doesn't disable the call to didSelectRowAtIndexPath. Set the selection style of cell to None in cellForRowAtIndexPath cell.selectionStyle = UITableViewCellSelectionStyleNone; Below snippet disable everything on the cell. This will disable the interaction to buttons, textfields: self.tableView.userInteractionEnabled = false;

迅速:

以下是上述Objective-C解决方案的Swift等效版本:

更换第一个溶液 self.tableView.allowsSelection = false 更换第二溶液 细胞?。selectionStyle = UITableViewCellSelectionStyle。没有一个 更换第三种溶液 self.tableView.userInteractionEnabled = false

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

objective - c:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3和4.x:

cell.selectionStyle = .none

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

更多信息在这里和这里