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


当前回答

在属性检查器中的UITableViewCell的XIB中,将Selection的值设置为None。

其他回答

场景- 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

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

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

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

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

我们可以编写这样的代码

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

但当我们有自定义单元格xib时,在该行上面给出警告

自定义单元格xib

我们需要从界面构建器中设置选择样式为None

快速解决方案,自定义单元:

import Foundation

class CustomTableViewCell: UITableViewCell
{
  required init(coder aDecoder: NSCoder)
  {
    fatalError("init(coder:) has not been implemented")
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.selectionStyle = UITableViewCellSelectionStyle.None
  } 
}

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

objective - c:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3和4.x:

cell.selectionStyle = .none

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

更多信息在这里和这里