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

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

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

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

谢谢。


当前回答

简单的

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

其他回答

实现方法tableView:willSelectRowAtIndexPath: 在表的数据源中。如果希望突出显示路径处的行,则返回给定的indexPath。如果没有,则返回nil。

示例来自我的应用程序:

- (NSIndexPath *)tableView:(UITableView *)tableView
    willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MySelectableObj* obj = [self objectAtPath:indexPath];
    if(obj==nil) return nil;
    return indexPath;
}

这样做的好处是,如果上面的方法返回nil, shouldPerformSegueWithIdentifier:sender:将不会被调用,尽管我只是为了完整性而重复上面的测试。

针对swift 4.0 这样就可以了。 它将禁用didSelectRowAtIndexPath方法中的Cell,但保持子视图可单击。

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
         if (indexPath.row == clickableIndex ) { 
            return indexPath
         }else{
            return nil
        }
    }

要停止选定某些单元格,请使用以下命令:

cell.userInteractionEnabled = NO;

除了阻止选择,这也会阻止设置了tableView:didSelectRowAtIndexPath的单元格被调用。

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

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

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

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

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

Edit01:

如果有一个表,其中只有一些行是可选择的,那么可选择行的单元格在视觉上与不可选择行的单元格区别就很重要。chevron附件按钮是默认的方法。

无论你怎么做,你不希望你的用户尝试选择行,并认为应用程序已经错误,因为行没有做任何事情。

我同意布莱恩的回答。

如果我用cell。isUserInteractionEnabled = false,那么单元格中的子视图将不会被用户交互。

另一方面,设置单元格。selectionStyle = .none将触发didSelect方法,尽管没有更新选择颜色。

使用willSelectRowAt是我解决问题的方法。例子:

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    switch (indexPath.section, indexPath.row) {
    case (0, 0), (1, 0): return nil
    default: return indexPath
    }
}