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

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

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

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

谢谢。


当前回答

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

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

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

其他回答

对于swift 3.0,你可以使用下面的代码禁用用户与UITableView单元格的交互

 cell.isUserInteractionEnabled = false

迅速:

cell.selectionStyle = .None
cell.userInteractionEnabled = false

如果你想让一行(或行子集)不可选,实现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: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:将不会被调用,尽管我只是为了完整性而重复上面的测试。

使用它可以使单元格看起来是禁用的,不可选择:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

重要:注意,这只是一个样式属性,实际上并没有禁用单元格。为了做到这一点,你必须检查selectionStylein你的didSelectRowAtIndexPath: delegate实现:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.selectionStyle == UITableViewCellSelectionStyleNone) {
        return;
    }

    // do your cell selection handling here
}