错误:UITableView跳到UITableViewAutomaticDimension顶部

我正在使用UITableViewestimatedRowHeightUITableViewAutomaticDimension 。 另外我正在使用NSFetchedResultsControllerDelegate来反映更改。

一切都很好。 现在的问题是,当我添加一些logging到CoreDataCoreData调用它的委托,但一个意想不到的事情发生。 每次TableView突然滚动到顶部。

NSFetchedResultsControllerDelegate

 func controllerWillChangeContent(controller: NSFetchedResultsController) { tableView.beginUpdates() } func controllerDidChangeContent(controller: NSFetchedResultsController) { tableView.endUpdates() } func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { switch type { case .Insert: tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None) break case .Update: tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None) break case .Delete: tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None) break default: break; } } 

通过谷歌search,我发现很less有人build议使用tableView: heightForRowAtIndexPath:但是因为我的单元格高度是dynamic的。 所以我该怎么做?

 tableView.reloadData() if tableView.numberOfRowsInSection(0) > 0 { let indexPath = NSIndexPath(forRow: 0, inSection: 0) self. tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) } 

添加到@ jayesh-miruliya的答案,在你的NSFetchedResultsControllerDelegate switch语句之后,把它放在你的代码中:

 tableView.reloadData() dispatch_async(dispatch_get_main_queue(), { let topCellIndexPath = NSIndexPath(forRow: 0, inSection: 0) self. tableView.scrollToRowAtIndexPath(topCellIndexPath, atScrollPosition: .Top, animated: true) }) 

为了解决我的问题,我保存在willDisplay方法和estimatedHeightForRowAt的单元格高度,我正在检索的值。

 var cellHeights = NSMutableDictionary() override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { if let height = cellHeights.object(forKey: indexPath) { return height as! CGFloat } return UITableViewAutomaticDimension } override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cellHeights.setObject(cell.frame.size.height, forKey: indexPath as NSCopying) } 

这是tableview的默认行为,在tableview中插入一行将其滚动到顶部。 我确实遇到了与我的应用程序相同的问题。 这里是我如何能够pipe理tableview的内容偏移,按照步骤,

1)在插入行或段之前存储UITableview的内容偏移量。 2)在数组中插入对象。 3加载桌面视图3)减去差值并手动设置内容偏移量。

 let oldOffset: CGFloat = tblTest.contentSize.height tblTest.reloadData() let newOffset: CGFloat = tblTest.contentSize.height tblTest.setContentOffset(CGPointMake(0, (newOffset - oldOffset)), animated: false) 

这是我迄今为止在我的应用程序中所做的工作,并且dynamic单元每次都能够克服重新初始化单元,所以它的工作非常棒。

  func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { switch type { case .Insert: tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None) break case .Update: tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None) break case .Delete: tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None) break default: break; } tableView.reloadData() let indexPath = NSIndexPath(forRow: 0, inSection: 0) self. tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) } } 
 let cou : Int = self.Array.count as Int let lastindex : NSIndexPath = NSIndexPath(forRow: cou-1, inSection: 0) self.TableName.scrollToRowAtIndexPath(lastindex, atScrollPosition: UITableViewScrollPosition.None, animated: true) 

这段代码可以帮助你在特定的位置滚动你的tableview。

 func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) tabelView.reloadData() } 

这是我的工作