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


当前回答

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

编码快乐!!

其他回答

你只需要把这段代码放到cellForRowAtIndexPath中

禁用单元格的选择属性:(在点击单元格时)。

cell.selectionStyle = UITableViewCellSelectionStyle.None

而这是最好的和最简单的解决方案,以防止一行在选择期间显示高亮

cell.selectionStyle = UITableViewCellSelectionStyleNone;

我还想建议,偶尔可以简单地显示已选中的行,然后将其关闭。这将提醒用户确认他们打算选择的内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
...
}

你也可以将背景颜色设置为Clear,以达到与UITableViewCellSelectionStyleNone相同的效果,以防你不想/不能使用UITableViewCellSelectionStyleNone。

您将使用如下代码:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

这可能会降低您的性能,因为您添加了一个额外的彩色视图到每个单元格。

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

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

你可以在(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


}