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


当前回答

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

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

OR

你可以到故事板>选择单元格>身份检查器>选择,从下拉菜单中选择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
  } 
}

编辑:对于更新的Swift,它被更改为:

cell.selectionStyle = .none

更多信息请看这个: https://developer.apple.com/documentation/uikit/uitableviewcell/selectionstyle

如果有人需要斯威夫特的答案:

cell.selectionStyle = .None

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

tableView.allowsSelection = false

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

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

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

tableView.allowsSelectionDuringEditing = false 

试着输入:

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的高亮显示

cell.selectionStyle = UITableViewCellSelectionStyleNone;

并且不应该允许用户与单元格交互。

cell.userInteractionEnabled = NO;