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

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

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

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

谢谢。


当前回答

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

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

其他回答

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

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

对于Xcode 6.3:

 cell.selectionStyle = UITableViewCellSelectionStyle.None;

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.selectionStyle = UITableViewCellSelectionStyleNone;

选择单元格(点击单元格)

// Default style
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

// Gray style
cell.selectionStyle = UITableViewCellSelectionStyleGray;

注意,单元格selectionStyle = UITableViewCellSelectionStyleNone;仍然会导致UI在用户触碰时调用didSelectRowAtIndexPath。为了避免这种情况,请按照下面的建议进行设置。

cell.userInteractionEnabled = NO;

代替。还要注意,你可能想要设置cell.textLabel.enabled = NO;使项目变灰。

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

在我的情况下,我是在行中间呈现一个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];

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

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