我正在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:
但因为我的单元格高度是动态的。所以我该怎么做?