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

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

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

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

谢谢。


当前回答

对于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) }
    }
}

实现方法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。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.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
}

你只需要把这段代码放到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;使项目变灰。