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


当前回答

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

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

其他回答

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

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

禁用UItableviewcell的高亮显示

cell.selectionStyle = UITableViewCellSelectionStyleNone;

并且不应该允许用户与单元格交互。

cell.userInteractionEnabled = NO;

我已经看过了所有的答案,在我的用例中起作用的是:

tableView.allowSelection = false
public func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    true
}

通过这种方式,表格保持可聚焦,用户可以滚动它的元素,但不能“按下/选择”它们。

简单地设置单元格。selectionStyle = .none将允许列表元素是可选择的(只是不留下灰色选择标记)。只是设置allowSelection = false会导致我的表无法聚焦。用户将无法滚动元素。

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

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

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


}