ios UITableview:如何禁用某些行而不是其他行的选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2267993/
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 16:55:31  来源:igfitidea点击:

UITableview: How to Disable Selection for Some Rows but Not Others

iosiphoneobjective-cuitableview

提问by Warrior

I am displaying in a group tableviewcontents parsed from XML. I want to disable the click event on it (I should not be able to click it at all) The table contains two groups. I want to disable selection for the first group only but not the second group. Clicking the first row of second group navigatesto my tube player view.

我在tableview从 XML 解析的组内容中显示。我想禁用它的点击事件(我根本不应该点击它) 该表包含两组。我只想禁用第一组的选择,而不是第二组。单击第二组的第一行navigates到我的管player view

How can I make just specific groups or rows selectable?

如何只选择特定的组或行?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if(indexPath.section!=0)
    if(indexPath.row==0)    

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tubeUrl]];   
}

Thanks.

谢谢。

回答by Pugal

You just have to put this code into cellForRowAtIndexPath

你只需要把这个代码放入 cellForRowAtIndexPath

To disable the cell's selection property: (while tapping the cell)

禁用单元格的选择属性:(同时点击单元格)

cell.selectionStyle = UITableViewCellSelectionStyleNone;

To enable being able to select (tap) the cell: (tapping the cell)

启用能够选择(点击)单元格:(点击单元格)

// Default style
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

// Gray style
cell.selectionStyle = UITableViewCellSelectionStyleGray;

Note that a cell with selectionStyle = UITableViewCellSelectionStyleNone;will still cause the UI to call didSelectRowAtIndexPathwhen touched by the user. To avoid this, do as suggested below and set.

请注意,当用户触摸时,带有的单元格selectionStyle = UITableViewCellSelectionStyleNone;仍会导致 UI 调用didSelectRowAtIndexPath。为避免这种情况,请按照以下建议进行设置。

cell.userInteractionEnabled = NO;

instead. Also note you may want to set cell.textLabel.enabled = NO;to gray out the item.

反而。另请注意,您可能希望cell.textLabel.enabled = NO;将该项目设置为灰色。

回答by Brian Chapados

If you want to make a row (or subset of rows) non-selectable, implement the UITableViewDelegatemethod -tableView:willSelectRowAtIndexPath:(also mentioned by TechZen). If the indexPathshould be not be selectable, return nil, otherwise return the indexPath. To get the default selection behavior, you just return the indexPathpassed to your delegatemethod, but you can also alter the row selection by returning a different indexPath.

如果您想让一行(或行的子集)不可选择,请实现该UITableViewDelegate方法-tableView:willSelectRowAtIndexPath:(TechZen 也提到过)。如果 不indexPath应该是可选的,则返回nil,否则返回indexPath。要获得默认选择行为,您只需返回indexPath传递给您的delegate方法,但您也可以通过返回不同的indexPath.

example:

例子:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( indexPath.row <= 2 ) return nil;

    // By default, allow row to be selected
    return indexPath;
}

回答by Keller

Starting in iOS 6, you can use

从 iOS 6 开始,您可以使用

-tableView:shouldHighlightRowAtIndexPath:

If you return NO, it disables both the selection highlighting and the storyboard triggered segues connected to that cell.

如果您 return NO,它将禁用选择突出显示和故事板触发连接到该单元格的 segues。

The method is called when a touch comes down on a row. Returning NOto that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.

当触摸出现在一行上时调用该方法。返回NO到该消息会停止选择过程,并且不会导致当前选定的行在按下触摸时丢失其选定外观。

UITableViewDelegate Protocol Reference

UITableViewDelegate 协议参考

回答by MightyMouse

None from the answers above really addresses the issue correctly. The reason is that we want to disable selection of the cell but not necessarily of subviews inside the cell.

上面的答案中没有一个真正正确地解决了这个问题。原因是我们想要禁用单元格的选择,但不一定是单元格内的子视图。

In my case I was presenting a UISwitch in the middle of the row and I wanted to disable selection for the rest of the row (which is empty) but not for the switch! The proper way of doing that is hence in the method

在我的例子中,我在行的中间展示了一个 UISwitch,我想禁用对行的其余部分(空的)的选择,而不是对开关的选择!因此,正确的方法是在方法中

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

where a statement of the form

表格的声明

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

disables selection for the specific cell while at the same time allows the user to manipulate the switch and hence use the appropriate selector. This is not true if somebody disables user interaction through the

禁用对特定单元格的选择,同时允许用户操纵开关并因此使用适当的选择器。如果有人通过

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

method which merely prepares the cell and does not allow interaction with the UISwitch.

仅准备单元格且不允许与 UISwitch 交互的方法。

Moreover, using the method

此外,使用该方法

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

in order to deselect the cell with a statement of the form

为了用表单的语句取消选择单元格

[tableView deselectRowAtIndexPath:indexPath animated:NO];

still shows the row being selected while the user presses on the original contentView of the cell.

当用户按下单元格的原始 contentView 时,仍然显示被选中的行。

Just my two cents. I am pretty sure many will find this useful.

只有我的两分钱。我很确定很多人会发现这很有用。

回答by TechZen

You trap selections with these data source methods.

您可以使用这些数据源方法捕获选择。

– tableView:willSelectRowAtIndexPath: 
– tableView:didSelectRowAtIndexPath: 
– tableView:willDeselectRowAtIndexPath: 
– tableView:didDeselectRowAtIndexPath:

In these methods, you check if the selected row is one you want to be selectable. If it is, take an action, if not, do nothing.

在这些方法中,您检查所选行是否是您想要选择的行。如果是,就采取行动,如果不是,什么都不做。

Unfortunately, you can't turn off selection for just one section. It's the whole table or nothing.

不幸的是,您不能仅关闭一个部分的选择。这是整张桌子或什么都没有。

You can however set the table cells selectionStyleproperty to UITableViewCellSelectionStyleNone. I believe that will make the selection invisible. Combined with the above methods that should make the cells look completely inert from the user's perspective.

但是,您可以将表格单元格selectionStyle属性设置为UITableViewCellSelectionStyleNone。我相信这将使选择不可见。结合上述方法,从用户的角度来看,应该使细胞看起来完全惰性。

Edit01:

编辑01:

If you have a table in which only some of the rows are selectable it is important that the cells of the selectable rows be visually distinct from the non-selectable rows. The chevron accessory button is the default way to do this.

如果您有一个表格,其中只有一些行是可选择的,重要的是可选择行的单元格在视觉上与不可选择的行不同。V 形附件按钮是执行此操作的默认方式。

However you do it, you don't want your users trying to select rows and thinking the app has malfed because the row doesn't do anything.

不管你怎么做,你都不希望你的用户试图选择行并认为应用程序出错了,因为行没有做任何事情。

回答by Alessandro Ornano

For Swift 4.0for example:

Swift 4.0为例:

cell.isUserInteractionEnabled = false
cell.contentView.alpha = 0.5

回答by azfar

You need to do something like the following to disable cell selection within the cellForRowAtIndexPathmethod:

您需要执行以下操作以禁用cellForRowAtIndexPath方法中的单元格选择:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setUserInteractionEnabled:NO];

To show the cell grayed out, put the following within the tableView:WillDisplayCell:forRowAtIndexPathmethod:

要将单元格显示为灰色,请将以下内容放入tableView:WillDisplayCell:forRowAtIndexPath方法中:

[cell setAlpha:0.5];

One method allows you to control the interactivity, the other allows you to control the UI appearance.

一种方法允许您控制交互性,另一种方法允许您控制 UI 外观。

回答by Abdul Rahman

You can use the tableView:willDisplayCellmethod to do all the kinds of customization to a tableViewCell.

您可以使用该tableView:willDisplayCell方法对 tableViewCell 进行各种自定义。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
     [cell setUserInteractionEnabled:NO];

     if (indexPath.section == 1 && indexPath.row == 0)
     {
         [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
         [cell setUserInteractionEnabled:YES];
     }
} 

In this above code, the user can only select the first row in the second section of the tableView. The rest all rows can't be selected. Thanks!~

在上面这段代码中,用户只能选择 tableView 的第二部分中的第一行。无法选择其余所有行。谢谢!~

回答by JosephH

To stop just some cells being selected use:

要停止仅选择某些单元格,请使用:

cell.userInteractionEnabled = NO;

As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set.

除了阻止选择外,这还会阻止 tableView:didSelectRowAtIndexPath: 为设置了它的单元格调用。

回答by Esqarrouth

For swift:

对于快速:

cell.selectionStyle = .None
cell.userInteractionEnabled = false