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

Saving reorder of TableView cells using NSCoding

我有一个 iOS 项目,我正在使用 Xcode7Swift2。我有一个 TableViewNSCoding 获取 array。我已启动它,因此用户可以在 TableView 中重新排序 TableViewCells。但是,我需要它在用户完成并单击 'done'(即 UIBarButtonItem)后保存新订单。我的 NSCoding object 中有一个名为 cellOrder 的值。我看了 here 并看到 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 转换为要保存的 NSDatamoveRowAtIndexPath 部分的代码现在是:

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()

}