我显示在一个组表视图内容解析从XML。我想禁用它上的单击事件(我应该不能单击它)表包含两个组。我只想禁用第一组的选择,而不是第二组。点击第二组的第一行导航到我的管播放器视图。

如何使特定的组或行可选?

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

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

谢谢。


当前回答

如果你想让一行(或行子集)不可选,实现UITableViewDelegate方法-tableView:willSelectRowAtIndexPath:(也被TechZen提到)。如果indexPath不可选,则返回nil,否则返回indexPath。要获得默认的选择行为,只需返回传递给委托方法的indexPath,但也可以通过返回不同的indexPath来更改行选择。

例子:

- (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;
}

其他回答

你可以使用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];
     }
} 

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

简单的

只需使用cell。userInteractionEnabled = YES;到单元格,如果它能导航和单元格。userInteractionEnabled = NO;否则

在Xcode 7上,无需编码,你可以简单地做到以下几点:

在大纲视图中,选择表视图单元格。 单元格嵌套在表视图控制器场景>表视图控制器>表视图下。你可能需要公开这些对象才能看到表格视图单元格)

在Attributes检查器中,找到标记为Selection的字段并选择None。 卡拉季奇检查员 有了这个选项,当用户点击单元格时,它将不会得到视觉突出显示。

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

在我的情况下,我是在行中间呈现一个UISwitch,我想禁用选择其余的行(这是空的),但不是为开关!正确的方法是在方法中

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

哪里有陈述的形式

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

禁用对特定单元格的选择,同时允许用户操作开关,从而使用适当的选择器。如果有人通过控件禁用用户交互,则不成立

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

方法,它只是准备单元,不允许与UISwitch交互。

此外,利用该方法

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

以取消选择具有窗体语句的单元格

[tableView deselectRowAtIndexPath:indexPath animated:NO];

当用户按下单元格的原始内容视图时,仍然显示所选择的行。

这只是我的个人意见。我敢肯定很多人会发现这很有用。

以Swift 4.0为例:

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