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

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

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

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

谢谢。


当前回答

实现方法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。userInteractionEnabled = YES;到单元格,如果它能导航和单元格。userInteractionEnabled = NO;否则

以Swift 4.0为例:

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

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

迅速:

cell.selectionStyle = .None
cell.userInteractionEnabled = false

我同意布莱恩的回答。

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