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


当前回答

你也可以通过在检查面板的选择选项(UITableView属性)中选择NoSelection来禁用界面构建器本身的行选择,如下图所示

其他回答

1-你所要做的就是设置UITableViewCell实例的选择样式:

objective - c:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

斯威夫特2:

cell.selectionStyle = UITableViewCellSelectionStyle.None


斯威夫特3:

cell.selectionStyle = .none

2 -不要实现- tableview:didSelectRowAtIndexPath:在你的表视图中委托或显式地排除你想要没有动作的单元格。

此外,你也可以从故事板。单击表视图单元格,在表视图单元格下的属性检查器中,将Selection旁边的下拉菜单更改为None。

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


}

这就是我所使用的,在cellForRowAtIndexPath中编写这段代码。

cell.selectionStyle = UITableViewCellSelectionStyleNone;

试着输入:

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中有一个控件禁止使用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一起工作,因此单元格不会在触摸事件时闪烁

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

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