为什么uitableview单元格保持突出显示?

什么会导致桌面单元格被触摸后保持突出显示? 我点击单元格,并可以看到它保持突出显示,因为细节视图被推动。 一旦细节视图popup,单元格仍然突出显示。

didSelectRowAtIndexPath您需要调用deselectRowAtIndexPath来取消select单元格。

所以无论你在做什么didSelectRowAtIndexPath你只需要调用deselectRowAtIndexPath

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Do some stuff when the row is selected [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

最干净的方法是viewWillAppear:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Unselect the selected row if any NSIndexPath* selection = [self.tableView indexPathForSelectedRow]; if (selection) { [self.tableView deselectRowAtIndexPath:selection animated:YES]; } } 

这样,当您返回到控制器时,您将看到淡出所选内容的animation。

取自http://forums.macrumors.com/showthread.php?t=577677

你有没有子类-(void)viewWillAppear:(BOOL)animated ? 当你不调用[super viewWillAppear:animated];时候,所select的UITableViewCell将不会被取消select[super viewWillAppear:animated]; 在你的自定义方法中。

对于Swift用户,将其添加到您的代码中:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) } 

这是除了在Swift而不是Obj-C之外的所有答案。

如果您使用的是UITableViewCell,请注释以下行

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { // [super setSelected:selected animated:animated]; } 

希望这可以帮助。

为了得到Kendall Helmstetter Gelner在他的评论中描述的行为,你可能不希望取消selectRowAtIndexPath,而是控制器上的clearsSelectionOnViewWillAppear属性。 也许这是偶然的?

有关新的UITableViewController子类,请参阅默认Apple模板中的注释:

 - (void)viewDidLoad { [super viewDidLoad]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; } 

Swift 3解决scheme

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath as IndexPath, animated: true) } 

我也遇到了这个问题,以及我的下钻应用程序。 在我将调用VC的viewcontroller之后,在推送另一个ViewController后返回,VC中的选定单元保持高亮显示。 在我的应用程序中,我创build了VC来处理钻取的第二层(三层)。

在我的情况下,问题是VC是一个UIViewController(包含一个视图,包含一个TableView)。 我反而使VC成为一个UITableViewController(包含一个TableView)。 UITableViewController类自动处理从推送返回之后的表格单元的去加亮。 第二个问题“ 在tableView中deselectRowAtIndexPath问题 ”这个post给出了一个更完整的答案。

这个问题没有发生在根视图控制器上,因为当我在XCode中创build应用程序作为“基于导航的应用程序”时,生成的根视图控制器已经作为子类UITableViewController。

我正在使用CoreData,所以对我来说代码是在各种答案的思想的组合,在Swift中:

 override func viewDidAppear(_ animated: Bool) { if let testSelected = yourTable.indexPathForSelectedRow { yourTable.deselectRow(at: testSelected, animated: true) } super.viewDidAppear(true) } 

我一直有同样的问题很长一段时间,以防其他人正在挣扎:

看看你的-tableView: cellForRowAtIndexPath:看看你是在创build单元格还是使用“重用标识符”。 如果是后者,请确保IB中的表格具有带有该标识符的单元格。 如果您没有使用重复使用的标识符,只需为每一行创build一个新的单元格。

这应该给你的表预期出现“淡入选定的行”。

在UITableViewCell类中使用此方法

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { // Just comment This line of code // [super setSelected:selected animated:animated]; } 

如果这些都不适合你,请考虑这个解决方法:

使用unwind segue来调用:

 @IBAction func unwind_ToTableVC (segue: UIStoryboardSegue) { if let index = tableView.indexPathForSelectedRow { tableView.deselectRowAtIndexPath(index, animated: true) } } 

为什么这样做? 主要是,如果您在取消select代码以在正确的时间运行时遇到问题。 我遇到了麻烦,不能在viewWillAppear工作,所以展开工作好多了。

脚步:

  1. 写下你的第一个VC(带表的那个)
  2. 去第二个VC。 控制 – 从你正在使用的取消/完成/等button拖动该VC并拖动到顶部的退出图标。
  3. select您在步骤1中创build的展开顺序

    祝你好运。

对于Swift 3:我宁愿它在viewDidDisappear中使用

定义: –

  var selectedIndexPath = IndexPath() 

在viewDidDisappear中: –

  override func viewDidDisappear(_ animated: Bool) { yourTableView.deselectRow(at: selectedIndexPath, animated: true) } 

在didSelectRowAtIndexPath中: –

  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { selectedIndexPath = indexPath }