我显示在一个组表视图内容解析从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;

其他回答

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

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

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

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

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

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

Edit01:

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

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

对于swift 3.0,你可以使用下面的代码禁用用户与UITableView单元格的交互

 cell.isUserInteractionEnabled = false

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

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

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
}