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


当前回答

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

cell.selectionStyle = .none

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

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

cell.selectionStyle = .None

其他回答

更好的方法是:

cell.userInteractionEnabled = NO;

这种方法不会调用didSelectRowAtIndexPath:方法。

禁用UItableviewcell的高亮显示

cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

cell.userInteractionEnabled = NO;

在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
  } 
}

我们可以编写这样的代码

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

自定义单元格xib

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