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


当前回答

我正在用这个,它对我很有用。

cell?.selectionStyle = UITableViewCellSelectionStyle.None

其他回答

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

编码快乐!!

你可以使用:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITableView的row at index path方法。

你还可以用:

[tableView deselectRowAtIndexPath:indexPath animated:NO];

在tableview中didselectrowatindexpath方法。

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

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

最好的解决方案是使选择样式为None

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

但是,这里我们考虑的事实是,所选状态没有使用自定义图像。

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

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