0

I have an iOS project I'm working on using Xcode7 and Swift2. I have a TableView that fetches an array from NSCoding. I have it started so the user can reorder the TableViewCells in the TableView. However I need it to save the new order once the user is finished and clicks 'done' which is a UIBarButtonItem. I have a value in my NSCoding object called cellOrder. I looked here and saw this for CoreData. How how would I do this for NSCoding and where do I save it?

I have the following code started for the TableViewCell movement:

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 is the array my data is kept in, using NSCoding.

Community
  • 1
  • 1
ChallengerGuy
  • 2,385
  • 6
  • 27
  • 43

1 Answers1

0

I decided the best way for me was to save the details array to NSUserDefaults after the the reorder was done. I saw here how to convert the NSObject to NSData to be saved. My code for the moveRowAtIndexPath section is now:

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

}
Community
  • 1
  • 1
ChallengerGuy
  • 2,385
  • 6
  • 27
  • 43