无法将 TableView 平滑地动画化到底部插入的新单元格
Cannot animate smoothly a TableView to new cells inserted at the bottom
我有一个 Swift 应用程序,它每隔几秒生成一次信息并将其添加到 ManagedObjectContext。
我有一个 table 视图,它实现了 NSFetchedResultsControllerDelegate 协议,在屏幕上显示新值。
值总是一个接一个出现,并且总是插入到 table 的底部。
单元格大小可变。
我需要的是保持平滑滚动到新插入的单元格,就像在消息传递应用程序中发生的那样。
我有以下代码:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView!.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
self.tableView!.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
self.insertedIndexPath = newIndexPath
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView!.endUpdates()
self.tableView!.scrollToRowAtIndexPath(self.insertedIndexPath!, atScrollPosition: .None, animated: animated)
}
代码工作正常,但是当 table 中有很多行时,滚动动画会在插入的每个新行中上下跳跃。
有人知道如何让这个动画运行平滑到新行吗?
谢谢,
GA
我找到了解决问题的方法。问题是我正在使用自动调整大小。
self.tableView!.rowHeight = UITableViewAutomaticDimension
self.tableView!.estimatedRowHeight = 150
我的 table 单元格的大小可能非常不同,自动调整大小总是开始将单元格大小调整为估计值。在添加新单元格之前,table 会自动滚动到根据估计计算出的位置。由于平均大小可能变化很大,table 滚动的估计位置总是错误的,这会疯狂地上下移动滚动条。
为了解决这个问题我设置了方法:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
我在设置视图内容之前计算大小。这样 table 根据单元格的实际大小计算滚动。
我希望它有所帮助。
GA
我有一个 Swift 应用程序,它每隔几秒生成一次信息并将其添加到 ManagedObjectContext。
我有一个 table 视图,它实现了 NSFetchedResultsControllerDelegate 协议,在屏幕上显示新值。
值总是一个接一个出现,并且总是插入到 table 的底部。
单元格大小可变。
我需要的是保持平滑滚动到新插入的单元格,就像在消息传递应用程序中发生的那样。
我有以下代码:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView!.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
self.tableView!.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
self.insertedIndexPath = newIndexPath
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView!.endUpdates()
self.tableView!.scrollToRowAtIndexPath(self.insertedIndexPath!, atScrollPosition: .None, animated: animated)
}
代码工作正常,但是当 table 中有很多行时,滚动动画会在插入的每个新行中上下跳跃。
有人知道如何让这个动画运行平滑到新行吗?
谢谢,
GA
我找到了解决问题的方法。问题是我正在使用自动调整大小。
self.tableView!.rowHeight = UITableViewAutomaticDimension
self.tableView!.estimatedRowHeight = 150
我的 table 单元格的大小可能非常不同,自动调整大小总是开始将单元格大小调整为估计值。在添加新单元格之前,table 会自动滚动到根据估计计算出的位置。由于平均大小可能变化很大,table 滚动的估计位置总是错误的,这会疯狂地上下移动滚动条。 为了解决这个问题我设置了方法:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
我在设置视图内容之前计算大小。这样 table 根据单元格的实际大小计算滚动。 我希望它有所帮助。 GA