ios - 使用 NSFetchedResultsController 移动行的方法及其委托(delegate)不起作用

标签 ios swift core-data tableview nsfetchedresultscontroller

我一直在努力关注this使用 NSFetchedResultsController 和 NSFetchedResultsController 委托(delegate)时在 TableView 中重新排列行的教程,但我的项目非常有问题。发生的事情是,当我尝试重新排列一行并将其删除时,这些行是随机的(可能不是随机的,但我看不到找到问题的模式)。我的实现有什么问题?

Bug

TableView 数据源:

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("RoutineCell", forIndexPath: indexPath) as! UITableViewCell
    var routine = fetchedResultsController.objectAtIndexPath(indexPath) as! Routine

    lastIndex++

    // Configure the cell...

    cell.textLabel?.text = routine.name

    return cell
}

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        var routine = fetchedResultsController.objectAtIndexPath(indexPath) as! Routine

        // Delete the row from the data source
        managedContext.deleteObject(routine)
        managedContext.save(nil)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext!

    if var routines = fetchedResultsController.fetchedObjects {

        let routine = routines[sourceIndexPath.row] as! Routine

        println("Instance of routine: \(routines[sourceIndexPath.row].description) created")

        routines.removeAtIndex(sourceIndexPath.row)

        println("Fetched data is: \(routines.description)")

        println("Routine removed at \(routines[sourceIndexPath.row])")

        routines.insert(routine, atIndex: destinationIndexPath.row)

        println("Routine inserted at index \(destinationIndexPath.row)")

        var idx: Int = Int(routines.count)
        for routine in routines as! [Routine] {
            routine.index = idx--
        }
        managedContext.save(nil)
    }

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        tableView.reloadRowsAtIndexPaths(tableView.indexPathsForVisibleRows()!, withRowAnimation: UITableViewRowAnimation.Fade)
    })
}

NSFetchedResultsControllerDelegate 方法:

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

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
}

func controller(
    controller: NSFetchedResultsController,
    didChangeObject anObject: AnyObject,
    atIndexPath indexPath: NSIndexPath?,
    forChangeType type: NSFetchedResultsChangeType,
    newIndexPath: NSIndexPath?) {

        // implementation to follow...

        switch type {
        case .Insert:
            // Note that for Insert, we insert a row at the __newIndexPath__
            if let insertIndexPath = newIndexPath {
                self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: .Fade)
            }
        case .Delete:
            // Note that for Delete, we delete the row at __indexPath__
            if let deleteIndexPath = indexPath {
                self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: .Fade)
            }
        case .Update:
            // Note that for Update, we update the row at __indexPath__
            if let updateIndexPath = indexPath {
                let cell = self.tableView.cellForRowAtIndexPath(updateIndexPath)
                let routine = self.fetchedResultsController.objectAtIndexPath(updateIndexPath) as? Routine

                cell?.textLabel?.text = routine?.name
            }
        case .Move:
            // Note that for Move, we delete the row at __indexPath__
            if let deleteIndexPath = indexPath {
                self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: .Fade)
            }

            // Note that for Move, we insert a row at the __newIndexPath__
            if let insertIndexPath = newIndexPath {
                self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: .Fade)
            }
        }
}

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

我做了与上面 gif 中相同的 Action :

Console output Console output 2

最佳答案

您更新数据模型的例程有点难读。您将最高的 index 分配给列表顶部的记录,倒数到 1 并且可能是反向排序......好的,让我们假设它正在工作。 *)

也许您应该在处理用户生成的数据更改时简单地禁用委托(delegate)方法?将您的操作括起来,包括保存方式

self.fetchedResultsController.delegate = nil
// ...
self.fetchedResultsController.delegate = self

*) 我认为,如果您最初是递增计数,然后重新排序递减计数,而获取的结果 Controller 递增计数,您会得到图像中所示的那种错误。

关于ios - 使用 NSFetchedResultsController 移动行的方法及其委托(delegate)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960079/

相关文章:

ios - Xcode 构建在设备上运行但在模拟器上失败

ios - 在运行时设置图像时不遵守 UIImageView 的内容模式

ios - iPad:在屏幕上保留选定的表格 View 单元格

ios - viewDidLoad 调用两次,使用导航 Controller

swift - 如何通过 NSUserDefaults 保存/加载集合类型?

ios - 在 ios swift 2 中将数据从 View Controller 传递到 TableView Controller

ios - 核心数据迁移 - 我们可以在 xcode 中拥有多个 .xcmappingmodel 文件吗?

iphone - App Store审核多种应用内购买方法

ios - Swift 无法在 Xcode 测试中测试核心数据?

ios - com.apple.gamed 在删除游戏时崩溃