UITableview:如何禁用某些行而不是其他的select
我显示在从XMLparsing的组tableview内容。 我想禁用它的点击事件(我不应该能够点击它)表中包含两个组。 我想禁用第一组的select而不是第二组。 点击第二组的第一行navigates到我的pipeplayer view 。 
我怎样才能使特定的组或行可选?
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section!=0) if(indexPath.row==0) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tubeUrl]]; } 
谢谢。
 你只需要把这个代码放到cellForRowAtIndexPath 
 禁用单元格的selectproperty:点击单元格时)。 
 cell.selectionStyle = UITableViewCellSelectionStyle.none 
 为了能够select(点击) cell:点击单元格)。 
 // Default style cell.selectionStyle = UITableViewCellSelectionStyle.blue // Gray style cell.selectionStyle = UITableViewCellSelectionStyle.gray 
 请注意,具有selectionStyle = UITableViewCellSelectionStyleNone的单元格仍将导致UI在用户触摸时调用didSelectRowAtIndexPath 。 为了避免这种情况,请按照下面的build议进行设置。 
 cell.userInteractionEnabled = false 
 代替。 另外请注意,您可能希望将cell.textLabel.enabled = false设置为灰色。 
如果你想使行(或行的子集)不可选,实现UITableViewDelegate方法-tableView:willSelectRowAtIndexPath 🙁 TechZen也提到)。 如果indexPath不可选,则返回nil,否则返回indexPath。 要获得默认select行为,只需返回传递给委托方法的indexPath,但也可以通过返回不同的indexPath来更改行select。
例:
 - (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; } 
从iOS 6开始,您可以使用
 -tableView:shouldHighLightRowAtIndexPath: 
 如果您返回NO ,则会禁用select突出显示以及连接到该单元格的故事板触发的segues。 
 触摸在一行中下来时调用该方法。 将NO返回到该消息将暂停select过程,并且不会导致当前选定的行在触摸closures时失去其选定的外观。 
UITableViewDelegate协议参考
没有从上面的答案真的解决了这个问题。 原因是我们想禁用单元格的select,但不一定是单元格内的子视图。
在我的情况下,我在行的中间提出了一个UISwitch,我想禁用行的其余部分(这是空的)的select,但不是交换机! 这样做的正确方法就是这个方法
 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
哪里有表格的说明
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
禁用对特定单元的select,同时允许用户操纵开关并因此使用适当的select器。 这是不正确的,如果有人通过禁用用户交互
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
方法只准备单元,不允许与UISwitch交互。
而且,使用该方法
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
以便用表单的声明取消select单元格
 [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
仍然显示当用户按下单元格的原始内容视图时被select的行。
只是我的两分钱。 我很确定很多人会觉得这很有用。
您使用这些数据源方法来捕获select。
 – tableView:willSelectRowAtIndexPath: – tableView:didSelectRowAtIndexPath: – tableView:willDeselectRowAtIndexPath: – tableView:didDeselectRowAtIndexPath: 
在这些方法中,你检查选中的行是否是你想要select的行。 如果是,采取行动,如果没有,什么也不做。
不幸的是,你不能只closures一个部分的select。 这是整个桌子或什么都没有。
 但是,您可以将表格单元格的selectionStyle属性设置为UITableViewCellSelectionStyleNone 。 我相信会使select隐形。 结合以上的方法,应该使用户的angular度看起来完全惰性的细胞。 
Edit01:
如果您有一个表中只有一些行是可select的,则可选行的单元格与非可选行在视觉上是不同的。 V形配件button是执行此操作的默认方式。
然而,你这样做,你不希望你的用户试图select行,并认为该应用程序已经发生故障,因为行不做任何事情。
 您需要执行以下操作来禁用cellForRowAtIndexPath方法中的单元格select: 
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setUserInteractionEnabled:NO]; 
 要显示单元格变灰,将以下内容放在tableView:WillDisplayCell:forRowAtIndexPath方法: 
 [cell setAlpha:0.5]; 
一种方法允许您控制交互性,另一种方法允许您控制UI外观。
以Swift 2.0为例:
 cell.userInteractionEnabled = false cell.contentView.alpha = 0.5 
要停止一些被选中的单元格,请使用:
 cell.userInteractionEnabled = NO; 
除了防止select,这也停止tableView:didSelectRowAtIndexPath:被设置的单元格被调用。
 你可以使用tableView:willDisplayCell方法来做一个tableViewCell的所有types的定制。 
 - (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]; } } 
在上面的代码中,用户只能selecttableView第二部分的第一行。 剩下的所有行都不能被选中。 谢谢!〜
为了迅速:
 cell.selectionStyle = .None cell.userInteractionEnabled = false 
我的解决scheme基于数据模型:
 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 } 
简单
 只要使用cell.userInteractionEnabled = YES; 到单元格,如果它可以导航和cell.userInteractionEnabled = NO; 除此以外 
我喜欢上面的Brian Chapados。 但是,这意味着您可能在cellForRowAtIndexPath和willSelectRowAtIndexPath中都有重复的逻辑,这很容易不同步。 不要复制逻辑,只需检查selectionStyle:
 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.selectionStyle == UITableViewCellSelectionStyleNone) return nil; else return indexPath; } 
我发现这个方便,因为它适用于静态和dynamic表。 我只在我希望允许select的单元上设置披露指标。
 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType != UITableViewCellAccessoryDisclosureIndicator) { return nil; } return indexPath; } 
在没有编码的Xcode 7上,您可以简单地执行以下操作:
在大纲视图中,select“表视图单元格”。 (单元格嵌套在“表视图控制器场景”>“表视图控制器”>“表视图”下,您可能必须披露这些对象以查看表视图单元格)
在“属性”检查器中,find标记为“select”的字段并select“无”。 属性检查器使用此选项,单元格在用户点击时不会获得视觉突出显示。
使用它来使单元看起来像被禁用和不可选:
 cell.selectionStyle = UITableViewCellSelectionStyleNone; 
 重要提示:请注意,这只是一个样式属性,并不会真正禁用单元格。 为了做到这一点,你必须在didSelectRowAtIndexPath: delegate实现中检查selectionStyle : 
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if(cell.selectionStyle == UITableViewCellSelectionStyleNone) { return; } // do your cell selection handling here } 
 在你的表的数据源中只实现tableView:willSelectRowAtIndexPath:方法。 如果要使path中的行突出显示,请返回给定的indexPath。 如果你不这样做,则返回零。 
从我的应用程序示例:
 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { MySelectableObj* obj = [self objectAtPath:indexPath]; if(obj==nil) return nil; return indexPath; } 
 关于这件事的shouldPerformSegueWithIdentifier:sender:是,如果上面的方法返回nil,那么shouldPerformSegueWithIdentifier:sender:不会被调用,尽pipe为了完整性我重复上面的testing。 
对于Xcode 6.3:
  cell.selectionStyle = UITableViewCellSelectionStyle.None; 
对我来说,以下工作正常:
 tableView.allowsSelection = NO; 
对于swift 3.0,你可以使用下面的代码来禁用用户交互到UITableView单元格
  cell.isUserInteractionEnabled = false