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

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

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

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

谢谢。


当前回答

使用这些数据源方法捕获选择。

– tableView:willSelectRowAtIndexPath: 
– tableView:didSelectRowAtIndexPath: 
– tableView:willDeselectRowAtIndexPath: 
– tableView:didDeselectRowAtIndexPath:

在这些方法中,您将检查所选行是否是您想要选择的行。如果是,采取行动,如果不是,什么都不做。

不幸的是,您不能只关闭一个部分的选择。要么整张桌子,要么什么都没有。

但是你可以将表格单元格的selectionStyle属性设置为UITableViewCellSelectionStyleNone。我相信这会让选择变得不可见。结合上述方法,应该使细胞从用户的角度看完全惰性。

Edit01:

如果有一个表,其中只有一些行是可选择的,那么可选择行的单元格在视觉上与不可选择行的单元格区别就很重要。chevron附件按钮是默认的方法。

无论你怎么做,你不希望你的用户尝试选择行,并认为应用程序已经错误,因为行没有做任何事情。

其他回答

我同意布莱恩的回答。

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

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

cell.selectionStyle = UITableViewCell.SelectionStyle.none

如果你想让一行(或行子集)不可选,实现UITableViewDelegate方法-tableView:willSelectRowAtIndexPath:(也被TechZen提到)。如果indexPath不可选,则返回nil,否则返回indexPath。要获得默认的选择行为,只需返回传递给委托方法的indexPath,但也可以通过返回不同的indexPath来更改行选择。

例子:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( indexPath.row <= 2 ) return nil;

    // By default, allow row to be selected
    return indexPath;
}

对于Xcode 6.3:

 cell.selectionStyle = UITableViewCellSelectionStyle.None;

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