Ios 带有NSFetchedResultsControllerDelegate更新的UISwipeActionsConfiguration
我有一个UITableViewDataSource,它也是一个NSFetchedResultsControllerDelegate 我在表的委托上实现:(这里故意省略completionHandler) 在数据源上,我将获取的结果控制器委托如下所示:Ios 带有NSFetchedResultsControllerDelegate更新的UISwipeActionsConfiguration,ios,swift,uitableview,nsfetchedresultscontroller,Ios,Swift,Uitableview,Nsfetchedresultscontroller,我有一个UITableViewDataSource,它也是一个NSFetchedResultsControllerDelegate 我在表的委托上实现:(这里故意省略completionHandler) 在数据源上,我将获取的结果控制器委托如下所示: extension UserPrescriptionsTableViewDataSource: NSFetchedResultsControllerDelegate { // MARK: - NSFetchedResultsControlle
extension UserPrescriptionsTableViewDataSource: NSFetchedResultsControllerDelegate {
// MARK: - NSFetchedResultsControllerDelegate
func controllerWillChangeContent(
_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView?.beginUpdates()
}
func controller(
_ controller: NSFetchedResultsController<NSFetchRequestResult>,
didChange anObject: Any,
at indexPath: IndexPath?,
for type: NSFetchedResultsChangeType,
newIndexPath: IndexPath?) {
switch type {
case .delete:
guard let path = indexPath else { return }
tableView?.deleteRows(at: [path], with: .automatic)
case .insert:
guard let newPath = newIndexPath else { return }
tableView?.insertRows(at: [newPath], with: .automatic)
case .move:
guard let path = indexPath, let newPath = newIndexPath else { return }
tableView?.deleteRows(at: [path], with: .automatic)
tableView?.insertRows(at: [newPath], with: .automatic)
case .update:
guard let path = indexPath else { return }
tableView?.reloadRows(at: [path], with: .automatic)
}
}
func controllerDidChangeContent(
_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView?.endUpdates()
}
}
我的问题是,滑动动画在使用中感觉正确,当我在操作完成时返回false时,代理确实删除了行,但它与用户的操作不匹配。在试用期间,我通过以下方式避免重复呼叫:
我试过:
- 我让数据源询问表的委托是否应该处理从获取的结果控制器委托发出的调用-结果正常,但在某些边缘情况下会出现不一致异常(我想继续沿着这条路走下去,但想先问一个问题,以防我把小题大做变成小题大做:)
- 忽略这个问题,但它不会消失你所说的用户行为是什么意思?实际的刷卡本身,当在处理程序中提供true时,感觉行的删除与用户的滑动非常匹配。那么为什么要发送false?以便获取的结果控制器委托能够处理行的删除。@FredFaust我理解您的意思-您在这件事上得出了什么结论?用户操作是什么意思很好?当在处理程序中提供true时,实际的刷卡本身感觉行的删除与用户的刷卡很好地匹配。那么为什么要发送false?以便获取的结果控制器委托能够处理行的删除。@FredFaust我明白你的意思-你在这件事上得出了什么结论?
extension UserPrescriptionsTableViewDataSource: NSFetchedResultsControllerDelegate { // MARK: - NSFetchedResultsControllerDelegate func controllerWillChangeContent( _ controller: NSFetchedResultsController<NSFetchRequestResult>) { tableView?.beginUpdates() } func controller( _ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { switch type { case .delete: guard let path = indexPath else { return } tableView?.deleteRows(at: [path], with: .automatic) case .insert: guard let newPath = newIndexPath else { return } tableView?.insertRows(at: [newPath], with: .automatic) case .move: guard let path = indexPath, let newPath = newIndexPath else { return } tableView?.deleteRows(at: [path], with: .automatic) tableView?.insertRows(at: [newPath], with: .automatic) case .update: guard let path = indexPath else { return } tableView?.reloadRows(at: [path], with: .automatic) } } func controllerDidChangeContent( _ controller: NSFetchedResultsController<NSFetchRequestResult>) { tableView?.endUpdates() } }
completionHandler(true) // animates row removal completionHandler(false) // does not remove the row