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

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

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

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

谢谢。


当前回答

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

 cell.isUserInteractionEnabled = false

其他回答

你可以使用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的第二部分中的第一行。其余所有行都不能被选中。谢谢!~

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外观。

我发现这很方便,因为它既适用于静态表,也适用于动态表。我只在那些我想允许选择的单元格上设置了显示指示符。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) {
        return nil;
    }
    return indexPath;
}

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

 cell.isUserInteractionEnabled = false