gpt4 book ai didi

ios - NSFetchedResultsControllerDelegate 在错误的 indexPath 上动画删除

转载 作者:行者123 更新时间:2023-11-28 08:54:47 24 4
gpt4 key购买 nike

前提:我有一个符合NSFetchedResultsControllerDelegateUITableViewController。我还有一个获取结果 Controller 和托管对象上下文作为 Controller 中的变量。我的 tableView 显示一个表格,其中包含来自获取的结果 Controller 的一部分核心数据对象。

我要实现的是滑动删除。选择删除的对象实际上被删除了,但是错误的 indexPath 正在被动画删除,我不知道为什么。我目前有以下我认为相关的方法:

// This method is being called in viewDidLoad, adding all of the CoreData objects to an array called fetchedResults.

func performFetch() {
do { try fetchedResultsController?.performFetch()
fetchedResults = fetchedResultsController?.fetchedObjects as! [Date]
} catch let error as NSError {
print(error.localizedDescription)
}
}

// tableViewDataSource methods

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let objectToDelete = fetchedResults[indexPath.row]
fetchedResultsController?.managedObjectContext.deleteObject(objectToDelete)

print("commitEditingStyle-indexPath = \(indexPath)")

do { try managedContext.save()
} catch let error as NSError {
print(error.localizedDescription)
}
}
}

// NSFetchedResultsControllerDelegate methods

func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject object: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Delete:
if let indexPath = indexPath {

print("didChangeObject indexPath = \(indexPath)")

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
default:
return
}
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}

如您所见,我打印了 tableView:commitEditingStyle 方法和 controller:didChangeObject 方法的 indexPath。这是 2 个打印语句:

commitEditingStyle-indexPath = {length = 2, path = 0 - 3}

didChangeObject-indexPath = {length = 2, path = 0 - 0}

为什么 didChangeObject 方法选择了错误的 indexPath?当我轻扫以删除对象时,对象在正确的 indexPath 处被删除(在本例中为 3...),但是用于动画删除的 TableView 单元格是 indexPath 0(我的 TableView 中的第一个单元格)。给了什么?

最佳答案

从您的代码中删除所有对 fetchedResults 的使用。您正在缓存 FRC 知道的初始对象集,但您没有跟踪该缓存中的添加或删除。缓存也是一种内存浪费,因为您始终可以从 FRC 中准确获取您想要的内容,并且它还会跟踪更改。

因此,您看到的应该是随机差异,是由于缓存数组和 FRC 之间的索引差异造成的。它们最初应该匹配,如果您只删除最后一个项目应该没问题,但任何其他删除都会导致它们不同步。

关于ios - NSFetchedResultsControllerDelegate 在错误的 indexPath 上动画删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com