I have a tableView with NSFetchedResultsController that listen for item updates.
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()
}
When user add item i update it in database every 1-2 seconds and i can't scroll down, because controller refresh item and scroll me to top.
I found problem. It is because i use:
self.tableView.rowHeight = UITableViewAutomaticDimension
Thanks for this answer: UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With