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


当前回答

因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。


所以,根据你想要的逻辑和/或结果,实际上有5个不同的答案:

1.禁用蓝色高亮,而不改变单元格的任何其他交互作用:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

我使用这个当我有一个UIButton -或其他一些控件(s) -托管在UITableViewCell,我希望用户能够与控件交互,而不是单元格本身。

注意:正如Tony Million上面提到的,这并不阻止tableView:didSelectRowAtIndexPath:。我通过简单的“if”语句来解决这个问题,最常见的是测试部分并避免对特定部分执行操作。

我想到的另一种测试敲击单元格的方法是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // A case was selected, so push into the CaseDetailViewController
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        // Handle tap code here
    }
}

2.要对整个表执行此操作,您可以将上述解决方案应用到表中的每个单元格,但您也可以这样做:

[tableView setAllowsSelection:NO];

在我的测试中,这仍然允许UITableViewCell内的控件是交互式的。 3.要使单元格为“只读”,你可以简单地这样做:

[cell setUserInteractionEnabled:NO];

4.将整个表设置为“只读”

[tableView setUserInteractionEnabled:NO];

5.要立即确定是否突出显示一个单元格(根据这个答案隐式地包括选择),你可以实现以下UITableViewDelegate协议方法:

- (BOOL)tableView:(UITableView *)tableView 
   shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

其他回答

我们可以编写这样的代码

 cell.selectionStyle = UITableViewCellSelectionStyleNone;

但当我们有自定义单元格xib时,在该行上面给出警告

自定义单元格xib

我们需要从界面构建器中设置选择样式为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


}
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:NO];
[cell setHighlighted:NO animated:NO];

编码快乐!!

在UITableViewDataSource协议中,在方法cellForRowAt中添加:

let cell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)                
cell.selectionStyle = .none
return cell

OR

你可以到故事板>选择单元格>身份检查器>选择,从下拉菜单中选择none。

因为我最近读了这篇文章,它对我有帮助,我想发布另一个答案来巩固所有的答案(为子孙后代)。


所以,根据你想要的逻辑和/或结果,实际上有5个不同的答案:

1.禁用蓝色高亮,而不改变单元格的任何其他交互作用:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

我使用这个当我有一个UIButton -或其他一些控件(s) -托管在UITableViewCell,我希望用户能够与控件交互,而不是单元格本身。

注意:正如Tony Million上面提到的,这并不阻止tableView:didSelectRowAtIndexPath:。我通过简单的“if”语句来解决这个问题,最常见的是测试部分并避免对特定部分执行操作。

我想到的另一种测试敲击单元格的方法是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // A case was selected, so push into the CaseDetailViewController
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        // Handle tap code here
    }
}

2.要对整个表执行此操作,您可以将上述解决方案应用到表中的每个单元格,但您也可以这样做:

[tableView setAllowsSelection:NO];

在我的测试中,这仍然允许UITableViewCell内的控件是交互式的。 3.要使单元格为“只读”,你可以简单地这样做:

[cell setUserInteractionEnabled:NO];

4.将整个表设置为“只读”

[tableView setUserInteractionEnabled:NO];

5.要立即确定是否突出显示一个单元格(根据这个答案隐式地包括选择),你可以实现以下UITableViewDelegate协议方法:

- (BOOL)tableView:(UITableView *)tableView 
   shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath