我正在四处寻找类似的情况,但是找不到包含我需要的所有功能的任何东西。环顾四周,我编写了如下所示的方法moveRowAtIndexPath的实现,但是在修改对coredata对象的引用时遇到了一些问题,因此当我单击完成时,coredata上没有任何保存。

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    userDrivenDataModelChange = true;

    if (sourceIndexPath == destinationIndexPath) {
        return
    }

    let rooms = fetchedResultsController.sections!

    let sourceRoom = rooms[sourceIndexPath.section]
    let destinationRoom = rooms[destinationIndexPath.section]

    var sourceLights = sourceRoom.objects as! [Light]
    var destinationLights = destinationRoom.objects as! [Light]
    let light = sourceLights[sourceIndexPath.row]

    sourceLights.removeAtIndex(sourceIndexPath.row)
    destinationLights.insert(light, atIndex: destinationIndexPath.row)

    (destinationLights as NSArray).enumerateObjectsUsingBlock({ object, index, stop in
        let light = object as! Light
        light.position = index
    })

    tableView.reloadData()

    Utils.saveContext(getAppDelegate().context)

    userDrivenDataModelChange = false;
}

最佳答案

缺少的部分是将新房间设置为我要移动的项目。就像是:

item.relationRoom = <destination room object>


当您在同一房间内移动灯光时,也需要进行一些调整。

08-17 09:31