当你点击UITableView中的一行时,该行会高亮显示并被选中。是否有可能禁用这个,所以点击一行什么都不做?


当前回答

你也可以将背景颜色设置为Clear,以达到与UITableViewCellSelectionStyleNone相同的效果,以防你不想/不能使用UITableViewCellSelectionStyleNone。

您将使用如下代码:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

这可能会降低您的性能,因为您添加了一个额外的彩色视图到每个单元格。

其他回答

根据我自己的实践经验,总结一下我认为的正确答案:

如果你想禁用部分单元格的选择,请使用:

cell.userInteractionEnabled = NO;

除了阻止选择,这也会阻止设置了tableView:didSelectRowAtIndexPath的单元格被调用。(感谢Tony Million的回答,谢谢!)

如果你的单元格中有需要点击的按钮,你需要改为:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

并且你还需要忽略单元格中的任何点击- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。

如果你想禁用整个表的选择,使用:

tableView.allowsSelection = NO;

(感谢保罗·德·巴罗斯!)

你也可以将背景颜色设置为Clear,以达到与UITableViewCellSelectionStyleNone相同的效果,以防你不想/不能使用UITableViewCellSelectionStyleNone。

您将使用如下代码:

UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = [UIColor clearColor];
backgroundColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView: backgroundColorView];

这可能会降低您的性能,因为您添加了一个额外的彩色视图到每个单元格。

你可以用这个

cell.selectionStyle = UITableViewCellSelectionStyleNone;

在属性检查器中的UITableViewCell的XIB中,将Selection的值设置为None。

你也可以从故事板中完成。单击表视图单元格,在表视图单元格下的属性检查器中,将Selection旁边的下拉菜单更改为None。