【发布时间】:2016-08-28 08:30:27
【问题描述】:
我将UITableView 与estimatedRowHeight 和UITableViewAutomaticDimension 一起使用。我也使用NSFetchedResultsControllerDelegate 来反映这些变化。
一切正常。现在的问题是,当我向CoreData 添加一些记录时,NSFetchedResultsController 称为它的委托,但发生了意想不到的事情。 TableView 每次都会突然滚动到 Top。
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;
}
}
通过谷歌搜索,我发现很少有人建议使用 tableView: heightForRowAtIndexPath: 的 answers,但因为我的单元格高度是动态的。所以我该怎么做?
【问题讨论】:
-
前段时间我已经回答了类似的问题:stackoverflow.com/a/30126655/826716
-
尝试实现方法, - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } 和 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; }
-
@SanuS 感谢您的建议。它已经在那里了。
-
您找到解决方案了吗?
-
@guillaume 还没有。 :(
标签: ios swift uitableview core-data uitableviewautomaticdimension