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

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

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

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

谢谢。


当前回答

appart from tableView。allowmultipleselection = true当选择时,你需要取消选择部分的其余单元格:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section does not support multiple selection {
        // deselect the rest of cells
        tableView
            .indexPathsForSelectedRows?.compactMap { $0 }
            .filter { $0.section == indexPath.section && $0.row != indexPath.row }
            .forEach { tableView.deselectRow(at: $0, animated: true) }
    }
}

其他回答

你需要做如下的事情来禁用cellForRowAtIndexPath方法中的单元格选择:

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

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

[cell setAlpha:0.5];

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

我喜欢Brian Chapados上面的回答。然而,这意味着你可能在cellForRowAtIndexPath和willSelectRowAtIndexPath中有重复的逻辑,这很容易失去同步。而不是复制逻辑,只需检查selectionStyle:

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

    else
        return indexPath;
}

我同意布莱恩的回答。

如果我用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
    }
}

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

cell.userInteractionEnabled = NO;

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

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

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
}