我有一个 tableView
和 NSFetchedResultsController
来监听项目更新。
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
if let _newIndexPath = newIndexPath {
self.tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .None)
}
case .Delete:
if let _indexPath = indexPath {
self.tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .None)
}
case .Update:
if let _indexPath = indexPath {
self.tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .None)
}
default:
self.tableView.reloadData()
}
self.items = controller.fetchedObjects as! [Item]
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
当用户添加项目时,我每 1-2 秒在数据库中更新一次,但我无法向下滚动,因为 Controller 刷新项目并将我滚动到顶部。
最佳答案
我发现了问题。这是因为我使用:
self.tableView.rowHeight = UITableViewAutomaticDimension
感谢您的回答:UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths
关于ios - 表更新时 NSFetchedResultsController 无法滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725884/