首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Swift)核心数据移动行-无效的更新错误

(Swift)核心数据移动行-无效的更新错误
EN

Stack Overflow用户
提问于 2018-05-19 01:14:51
回答 2查看 456关注 0票数 1

我试图在我的表视图中移动包含核心数据对象的行。我收到无效的更新错误。

更新无效:节0中的行数无效。更新后现有节中包含的行数(2)必须等于更新前该节中包含的行数(2),加上或减去从该节插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出)。

我不明白为什么我会得到这个。我删除了要在源索引路径上移动的行,然后将其插入到目标索引路径上。

下面是我的代码:

代码语言:javascript
复制
// Move fetchedResultsController objects
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let movedObject = self.fetchedResultsController.objectAtIndexPath(sourceIndexPath) as! NSManagedObject

    controller(self.fetchedResultsController, didChangeObject: movedObject, atIndexPath: sourceIndexPath, forChangeType: .Delete, newIndexPath: sourceIndexPath)

    controller(self.fetchedResultsController, didChangeObject: movedObject, atIndexPath: sourceIndexPath, forChangeType: .Insert, newIndexPath: destinationIndexPath)
}
EN

回答 2

Stack Overflow用户

发布于 2018-05-23 15:15:01

fetchedResultsController的任务是监视核心数据的变化,并告诉您的UI它需要更新以匹配核心数据中的内容。不调用控制器( didChangeObject:..方法;fetchedResultsController调用它们。

您应该只更新核心数据中的对象,等待fetchedResultsController通知您的viewController进行更新。

这一步要复杂得多。tableView已经更新了,现在您想要更新核心数据。但是当您更新核心数据时,fetchedResultsController将启动并告诉您执行此操作--您已经这样做了!因此,改为将fetchedResultsController设置为nil,然后更新核心数据(这将是fetch中存储对象的字段,可能是order属性或类似的属性),然后重置fetchedResultsController的委托。

票数 0
EN

Stack Overflow用户

发布于 2018-09-14 02:14:48

下面的代码是swift 4:在您有一个包含多个部分的表的情况下,moveRowAt fromIndexPath: IndexPath,to toIndexPath: IndexPath函数需要计算要移动的正确行。您不能只使用索引path.row (from & to),因为它们没有考虑到其他部分。我使用以下方法来获取正确的行:

代码语言:javascript
复制
    // Do not trigger delegate methods when changes are made to core data by the user
        fetchedResultsController.delegate = nil
        var fromIndex = fromIndexPath.row
        var toIndex = toIndexPath.row
        //      print ("from row ",fromIndexPath.row)
//work out correct row to remove at based on it's current row and section
        for sectionIndex in 0..<fromIndexPath.section
        {
            fromIndex += fetchedResultsController.sections![sectionIndex].numberOfObjects
        }
            //print ("fromIndex ",fromIndex)
// remove the row at it's old position
       toDoData.remove(at: fromIndex)
//work out the correct row to insert at based on which section it's going to & row in that section
        for sectionIndex in 0..<toIndexPath.section
        {
            toIndex += fetchedResultsController.sections![sectionIndex].numberOfObjects
            //print ("toIndex ",toIndex)
            if sectionIndex == fromIndexPath.section
            {
                toIndex -= 1 // Remember, controller still thinks this item is in the old section
                //print ("-= toIndex",toIndex)
            }
        }
// put the item back into he array at new position
         toDoData.insert(item, at: toIndex)

注意: toDoData是我从fetchedResultsController中获取的记录数组

注意:假设您在核心数据中保留了该部分的记录,并遍历数组以更新您的订单,则您还必须更新该部分。用法:- fetchedResultsController.sections.name

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50416308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档