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


当前回答

这就是我所使用的,在cellForRowAtIndexPath中编写这段代码。

cell.selectionStyle = UITableViewCellSelectionStyleNone;

其他回答

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

在UITableViewDelegate协议中,你可以使用willSelectRowAtIndexPath方法 如果你不希望行被选中,返回nil。

同样的,你可以使用willDeselectRowAtIndexPath方法并返回nil,如果你不想让行取消选择。

我已经看过了所有的答案,在我的用例中起作用的是:

tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    true
}

通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。

简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。

你可以在(iOS) Xcode 9, Swift 4.0中使用以下代码禁用表格单元格高亮显示

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTbCell") as! OpenTbCell
        cell.selectionStyle = .none
        return cell


}

你也可以通过在检查面板的选择选项(UITableView属性)中选择NoSelection来禁用界面构建器本身的行选择,如下图所示