Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift2 使用NSCoding保存TableView单元格的重新排序_Swift2_Tableview_Tableviewcell_Nscoding - Fatal编程技术网

Swift2 使用NSCoding保存TableView单元格的重新排序

Swift2 使用NSCoding保存TableView单元格的重新排序,swift2,tableview,tableviewcell,nscoding,Swift2,Tableview,Tableviewcell,Nscoding,我有一个iOS项目,我正在使用Xcode7和Swift2。我有一个TableView,它从NSCoding获取一个数组。我已经启动了它,因此用户可以在TableView中重新排序TableViewCells。但是,一旦用户完成并单击“完成”,我需要它来保存新订单,这是一个UIBarButtonItem。我的NSCoding对象中有一个名为celorder的值。我查看并看到了CoreData。如何为NSCoding执行此操作,以及在何处保存 我为TableViewCell移动启动了以下代码: fu

我有一个
iOS
项目,我正在使用
Xcode7
Swift2
。我有一个
TableView
,它从
NSCoding
获取一个
数组。我已经启动了它,因此用户可以在
TableView
中重新排序
TableViewCells
。但是,一旦用户完成并单击“完成”,我需要它来保存新订单,这是一个
UIBarButtonItem
。我的
NSCoding
对象中有一个名为
celorder
的值。我查看并看到了
CoreData
。如何为
NSCoding
执行此操作,以及在何处保存

我为
TableViewCell
移动启动了以下代码:

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


func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    let itemToMove = details[fromIndexPath.row]

    details.removeAtIndex(fromIndexPath.row)

    details.insert(itemToMove, atIndex: toIndexPath.row)

}

details
是我保存数据的数组,使用
NSCoding

我决定最好的方法是在重新排序后将
details
array
保存到
NSUserDefaults
。我了解了如何将
NSObject
转换为要保存的
NSData
。我的
moveRowatineXpath
部分的代码现在是:

func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    let itemToMove = details[fromIndexPath.row]

    details.removeAtIndex(fromIndexPath.row)

    details.insert(itemToMove, atIndex: toIndexPath.row)

    // Archive NSObject and save as NSData
    let myData = NSKeyedArchiver.archivedDataWithRootObject(details)

    // NSUserDefaults Save for Reorder of cells
    reportDefaults.setObject(myData, forKey: "savedReportListKEY")
    reportDefaults.synchronize()

    // NSCoding Save
    saveReport()

}