具有NSFetchedResultsControllerDelegate更新的UISwipeActionsConfiguration

时间:2019-02-11 18:02:36

标签: ios swift uitableview nsfetchedresultscontroller

我有一个UITableViewDataSource,它也是NSFetchedResultsControllerDelegate。

在表的委托上,我实现了:(此处故意省略了completionHandler)

func tableView(
    _ tableView: UITableView,
    trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in

      let object = self.object(at: indexPath)

      do {
        try ObjectManager.delete(object)
      } catch let error as NSError {
        print(error)
      }
    }

    let config = UISwipeActionsConfiguration(actions: [delete])
    config.performsFirstActionWithFullSwipe = true
    return config
  }

在数据源上,我遵循这样的获取结果控制器委托:

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()
  }
}

滑动操作更新通过或不通过在操作自己的块中调用的完成处理程序来更新UI:

completionHandler(true) // animates row removal
completionHandler(false) // does not remove the row

我的问题是,滑动动画使用起来感觉正确,当我在操作完成时返回false时,委托确实删除了该行,但与用户的操作不完全匹配。在试用中,我避免通过以下方式重复通话:

我尝试过:

  • 我问数据源,该表的委托人是否应处理来自提取的结果控制器委托的调用-可以解决,但在某些边缘情况下会出现不一致的异常(我想继续介绍一下)路径,但想先问一个问题,以防万一我将小丘变成山峰:))。

  • 忽略问题,但问题不会消失

0 个答案:

没有答案
相关问题