NSFetchedResultsControllerDelegate对错误的indexPath进行动画删除 [英] NSFetchedResultsControllerDelegate animating deletion on wrong indexPath

查看:33
本文介绍了NSFetchedResultsControllerDelegate对错误的indexPath进行动画删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The premise: I have a UITableViewController that conforms to NSFetchedResultsControllerDelegate. I also have a fetched results controller and managed object context as variables in the controller. My tableView displays a table with one section of core data objects from the fetched results controller.

我是什么尝试实施的方法是滑动删除。选择删除的对象实际上已被删除,但是错误的indexPath被动画删除,我也不知道为什么。我目前有以下几种我认为相关的方法:

What I'm trying to implement is swipe to delete. The object selected for deletion is actually deleted, however the wrong indexPath is being animated to delete and I don't know why. I currently have the following methods that I believe are relevant:

// 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 方法以及打印indexPath controller:didChangeObject 方法。以下是2条打印语句:

As you can see, I print the indexPath for the tableView:commitEditingStyle method as well as the controller:didChangeObject method. Here are the 2 print statements:

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

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

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

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

为什么 didChangeObject 方法选择错误的indexPath?当我滑动删除对象时,将在适当的indexPath处删除该对象(在本例中为3 ...),但动画删除的表视图单元格为indexPath 0(在我的表视图中的第一个单元格)。

Why is the didChangeObject method picking up the wrong indexPath? When I swipe to delete the object, the object is deleted at the proper indexPath (in this case 3...) but the table view cell that animates deletion is indexPath 0 (the first cell in my table view). What gives?

推荐答案

从代码中删除所有 fetchedResults 的用法。您正在缓存FRC知道的初始对象集,但没有在该缓存中跟踪添加或删除的对象。缓存也浪费了内存,因为您总是可以从FRC完全获得所需的内容,并且还可以跟踪更改。

Remove all usage of fetchedResults from your code. You are caching the initial set of objects that the FRC knows about, but you aren't tracking additions or removals in that cache. The cache is also a waste of memory because you can always get exactly what you want from the FRC and it also tracks changes.

因此,您所看到的应该是似乎是随机差异,是由于缓存阵列与FRC之间的索引差异所致。它们最初应该匹配,并且如果您只删除最后一个项目就可以,但是其他任何删除都将导致它们不同步。

So, what you're seeing should be what appears to be a random difference and is due to indexing differences between the cache array and the FRC. They should match initially, and if you only ever delete the last item it should be ok, but any other deletion would cause them to fall out of sync.

这篇关于NSFetchedResultsControllerDelegate对错误的indexPath进行动画删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆