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


当前回答

如果你想选择只闪光,而不是保持在选择状态,你可以调用,在

didSelectRowAtIndexPath

以下

[tableView deselectRowAtIndexPath:indexPath animated:YES];

它会刷新选中的状态并恢复。

其他回答

从ios6.0开始,UITableViewDelegate有tableView:shouldHighlightRowAtIndexPath:。(请参阅iOS文档。)

此方法允许您将特定行标记为不可突出显示(并且隐式地不可选择),而无需更改单元格的选择样式,也无需使用userInteractionEnabled = NO或这里记录的任何其他技术来打乱单元格的事件处理。

在属性检查器中的UITableViewCell的XIB中,将Selection的值设置为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中有一个控件禁止使用userInteractionEnabled属性。我有一个3单元静态表的设置,2与日期,1与开/关开关。在Storyboard/IB中,我成功地使底部不可选,但当你点击它时,从顶部行之一的选择消失了。这是我设置UITableView的WIP图像:

如果你点击第三行什么都没有发生,选择将停留在第二行。该功能实际上是苹果日历应用程序添加事件时间选择屏幕的副本。

代码是令人惊讶的兼容,一直到IOS2 =/:

- (NSIndexPath *)tableView: (UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return nil;
    }
    return indexPath;
}

这与设置选择样式为none一起工作,因此单元格不会在触摸事件时闪烁

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

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