Como impedir a rolagem de UITableView para cima quando o NSFetchedResultsController adiciona um novo registro?
MinhasUITableView
está rolando para cima quando adiciono um novo comentário ao CoreData. NSFetchedResultsController sabe disso, coloca esse comentário na parte inferior da tabela e role a tabela para cima. Isso é normal?
Meu exemplo:
Antes de adicionar um comentário:
Logo após adicionar um comentário (comportamento não esperado):
Deve ser assim (depois eu deslizo manualmente):
Isso pode estar conectado à seguinte situação:
Este é um descritor de classificação do meuNSFetchedResultsController
:
NSSortDescriptor(key: "createdAt", ascending: false) //from the latest to the oldest
Mas preciso exibir meus comentários para caminhos de índice invertidos (os mais recentes estão no fundo) Em outras palavras, em todos os lugares quando eu preciso usarindexPath
Eu uso:
private func reversedIndexPathForIndexPath(indexPath: NSIndexPath) -> NSIndexPath {
return NSIndexPath(forRow: fetchedResultsController.fetchedObjects!.count - indexPath.row - 1, inSection: 0)
}
NSFetchedResultsControllerDelegate
:
//MARK: - NSFetchedResultsControllerDelegate
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 {
tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
}
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
}
case .Update:
if let indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
}
case .Move:
if let indexPath = indexPath, let newIndexPath = newIndexPath {
tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
}
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
Está conectado ao meu problema?