ios 如何禁用 UITableView 选择?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/190908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:19:05  来源:igfitidea点击:

How can I disable the UITableView selection?

iosuitableviewcocoa-touch

提问by DavidM

When you tap a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?

当您点击 a 中的一行时UITableView,该行会突出显示并被选中。是否可以禁用此功能,以便点击一行没有任何作用?

采纳答案by DavidM

For me, the following worked fine:

对我来说,以下工作正常:

tableView.allowsSelection = false

This means didSelectRowAt#simply won't work. That is to say, touching a row of the table, as such, will do absolutely nothing. (And hence, obviously, there will never be a selected-animation.)

这意味着didSelectRowAt#根本行不通。也就是说,触摸桌子上的一行,绝对不会做任何事情。(因此,显然,永远不会有选定的动画。)

(Note that if, on the cells, you have UIButtonor any other controls, of course those controls will still work. Any controls you happen to have on the table cell, are totally unrelated to UITableView's ability to allow you to "select a row" using didSelectRowAt#.)

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

Another point to note is that: This doesn't work when the UITableViewis in editing mode. To restrict cell selection in editing mode use the code as below:

还有一点需要注意的是: 当UITableView处于编辑模式时,这不起作用。要在编辑模式下限制单元格选择,请使用以下代码:

tableView.allowsSelectionDuringEditing = false 

回答by Martin Gordon

All you have to do is set the selection style on the UITableViewCellinstance using either:

您所要做的就是UITableViewCell使用以下任一方法在实例上设置选择样式:

Objective-C:

目标-C:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

or

或者

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Swift 2:

斯威夫特 2:

cell.selectionStyle = UITableViewCellSelectionStyle.None

Swift 3 and 4.x:

Swift 3 和 4.x:

cell.selectionStyle = .none

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.

此外,请确保您要么不在-tableView:didSelectRowAtIndexPath:表视图委托中实施,要么在实施时明确排除您不想采取任何行动的单元格。

More info hereand here

更多信息在这里这里

回答by mbm29414

Because I've read this post recently and it has helped me, I wanted to post another answer to consolidate all of the answers (for posterity).


因为我最近阅读了这篇文章并且它对我有帮助,所以我想发布另一个答案来整合所有答案(供后代使用)。




So, there are actually 5 different answers depending on your desired logic and/or result:

因此,实际上有 5 种不同的答案,具体取决于您想要的逻辑和/或结果:

1.To disable the blue highlighting without changing any other interaction of the cell:

1.要禁用蓝色突出显示而不更改单元格的任何其他交互:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

I use this when I have a UIButton - or some other control(s) - hosted in a UITableViewCell and I want the user to be able to interact with the controls but not the cell itself.

当我有一个 UIButton - 或其他一些控件 - 托管在 UITableViewCell 中并且我希望用户能够与控件交互而不是单元格本身时,我会使用它。

NOTE: As Tony Million noted above, this does NOT prevent tableView:didSelectRowAtIndexPath:. I get around this by simple "if" statements, most often testing for the section and avoiding action for a particular section.

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

Another way I thought of to test for the tapping of a cell like this is:

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

- (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.To do this for an entire table, you can apply the above solution to each cell in the table, but you can also do this:



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

[tableView setAllowsSelection:NO];

In my testing, this still allows controls inside the UITableViewCellto be interactive.


3.To make a cell "read-only", you can simply do this:

在我的测试中,这仍然允许内部的控件UITableViewCell进行交互。


3.要使单元格“只读”,您可以简单地执行以下操作:

[cell setUserInteractionEnabled:NO];



4.To make an entire table "read-only"



4.使整个表“只读”

[tableView setUserInteractionEnabled:NO];



5.To determine on-the-fly whether to highlight a cell (which according to this answerimplicitly includes selection), you can implement the following UITableViewDelegateprotocol method:



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

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

回答by JosephH

To sum up what I believe are the correct answers based on my own experience in implementing this:

根据我自己的实施经验总结一下我认为的正确答案:

If you want to disable selection for just some of the cells, use:

如果您只想禁用某些单元格的选择,请使用:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)

除了阻止选择外,这还会阻止 tableView:didSelectRowAtIndexPath: 为设置了它的单元格调用。(感谢托尼·百万的回答,谢谢!)

If you have buttons in your cells that need to be clicked, you need to instead:

如果您的单元格中有需要点击的按钮,您需要改为:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

and you also need to ignore any clicks on the cell in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.

并且您还需要忽略对单元格中的任何点击- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

If you want to disable selection for the whole table, use:

如果要禁用整个表的选择,请使用:

tableView.allowsSelection = NO;

(Credit to Paulo De Barros, thanks!)

(感谢 Paulo De Barros,谢谢!)

回答by cbowns

As of iOS 6.0, UITableViewDelegatehas tableView:shouldHighlightRowAtIndexPath:. (Read about it in the iOS Documentation.)

从 iOS 6.0 开始,UITableViewDelegatetableView:shouldHighlightRowAtIndexPath:. (在 iOS文档中阅读它。)

This method lets you mark specific rows as unhighlightable (and implicitly, unselectable) without having to change a cell's selection style, messing with the cell's event handling with userInteractionEnabled = NO, or any other techniques documented here.

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

回答by vignesh kumar

You can also disable selection of row from interface builder itself by choosing NoSelectionfrom the selectionoption(of UITableView Properties) in inspector pane as shown in the below image

您还可以通过NoSelectionselection检查器窗格中的选项(UITableView 属性)中进行选择来禁用界面构建器本身的行选择,如下图所示

UITableView Inspector

UITableView 检查器

回答by MANISH PATHAK

FIXED SOLUTION FOR SWIFT 3

Swift 3 的固定解决方案

cell.selectionStyle = .none

回答by Zaid Pathan

In your UITableViewCell's XIBin Attribute Inspector set value of Selectionto None.

在属性检查器中UITableViewCellXIB中,将值设置为Selectionto None

enter image description here

在此处输入图片说明

回答by Chris Fox

If you want selection to only flash, not remain in the selected state, you can call, in

如果你想让选择只闪烁,而不是保持在选择状态,你可以调用,在

didSelectRowAtIndexPath

the following

下列

[tableView deselectRowAtIndexPath:indexPath animated:YES];

so it will flash the selected state and revert.

所以它会闪烁选择的状态并恢复。

回答by Aks

In case anyone needs answer for Swift:

如果有人需要Swift 的答案:

cell.selectionStyle = .None