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

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

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

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

谢谢。


当前回答

我基于数据模型的解决方案:

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    let rowDetails = viewModel.rowDetails(forIndexPath: indexPath)
    return rowDetails.enabled ? indexPath : nil
}

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    let rowDetails = viewModel.rowDetails(forIndexPath: indexPath)
    return rowDetails.enabled
}

其他回答

从iOS 6开始,你可以使用

-tableView:shouldHighlightRowAtIndexPath:

如果您返回NO,它将禁用选择高亮显示和故事板触发的连接到该单元格的segue。

该方法在触摸到一行时被调用。向该消息返回NO将停止选择过程,并且不会导致当前选定的行在触摸停止时失去选定的外观。

UITableViewDelegate协议引用

实现方法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:将不会被调用,尽管我只是为了完整性而重复上面的测试。

斯威夫特5: 在你的cellForRowAt函数中放置以下一行:

cell.selectionStyle = UITableViewCell.SelectionStyle.none

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

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
}

以Swift 4.0为例:

cell.isUserInteractionEnabled = false
cell.contentView.alpha = 0.5