6

当您点击 a 中的一行时UITableView,该行将突出显示并被选中。是否可以禁用此功能,因此点击一行什么也不做?

4

10 回答 10

18
cell.selectionStyle = UITableViewCellSelectionStyleNone;

or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.

于 2014-02-24T19:19:56.150 回答
4

实现tableView:willSelectRowAtIndexPath:委托方法UITableView并返回nil。从此方法返回 nil 告诉表视图不选择行,因此tableView:didSelectRowAtIndexPath:不会调用该方法。但是这样做并不能阻止突出显示该行。要禁用对行设置单元格选择样式的突出显示效果UITableViewCellSelectionStyleNone为在上述答案中进行了解释。

于 2013-03-21T08:36:54.697 回答
2

只需设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
于 2013-03-21T08:02:48.817 回答
2
cell.selectionStyle = UITableViewCellSelectionStyleNone;
于 2013-03-21T08:04:10.110 回答
1

如果您想停止突出显示,请使用代码说:您想要的值为 19

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{
if(indexpath.row == '19')
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleGray;

}

但是,如果你想控制单元格的选择

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

if(indexpath.row == '19')
[tableView deselectRowAtIndexPath:indexPath animated:NO];

}
于 2013-03-21T11:22:50.220 回答
1

对于斯威夫特 3:

cell.selectionStyle = UITableViewCellSelectionStyle.none
于 2018-03-06T03:21:21.077 回答
0

这是针对您有自定义单元格的情况,但我在寻找答案时发现了这个问题,所以我想我会把它留在这里以防这对其他人有帮助。


转到您的自定义单元格,并在方法中设置selectionStyle为:.noneawakeFromNib

override func awakeFromNib() {
    super.awakeFromNib()

    // disable selection
    selectionStyle = .none;
}

如果您在多个UITableViews 中使用此自定义单元格,则只需在自定义单元格中设置一次,而不是每次UITableView使用 :)

于 2018-09-04T17:40:34.313 回答
0

斯威夫特 4

cell.selectionStyle = UITableViewCell.SelectionStyle.none
于 2018-12-11T07:11:44.943 回答
0

您可以简单地为OBJ C执行此操作:

 cell.selectionStyle = UITableViewCellSelectionStyleNone;
于 2019-03-05T22:23:35.120 回答
0

接受的答案对我不起作用。这个问题让我发疯了一阵子,因为当下一个视图加载时,它看起来像任何选择的“故障”..

我的解决方案:我必须转到实际 TableViewCell(我构建的自定义单元格)的 .xib 文件选择单元格并选择 CellView。(确保选择单元格,而不是其中的子视图。它会变成蓝色,如图所示)。在左侧单击“选择”->“无”。这应该与使用代码完全相同,但由于某种原因它不起作用...... 在此处输入图像描述

于 2018-05-07T20:08:30.233 回答