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


当前回答

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

cell?.selectionStyle = UITableViewCellSelectionStyle.None

其他回答

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


}

你可以用这个

cell.selectionStyle = UITableViewCellSelectionStyleNone;

你可以使用UITableViewCell的selectionStyle属性

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

同样,不要实现下面的委托

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... }

如果你已经创建了Xib/Storyboard文件,那么你可以改变setUserInteractionEnabled 通过取消勾选tableview的属性为No。 这将使你的表视图为只读。

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


}

对我来说,以下方法效果不错:

tableView.allowsSelection = false

这意味着didselectrow#将无法工作。也就是说,触摸表中的一行,本身不会有任何作用。(因此,很明显,永远不会有选中动画。)

(请注意,如果在单元格上有UIButton或任何其他控件,当然这些控件仍然可以工作。你碰巧在表格单元格上的任何控件,都与UITableView允许你使用didSelectRowAt#“选择一行”的能力完全无关。)

另一点需要注意的是:当UITableView处于编辑模式时,这是无效的。要在编辑模式下限制单元格选择,请使用如下代码:

tableView.allowsSelectionDuringEditing = false