0

Currently, I have a to-do list app. When selecting a row, the alpha dims, indicating that the task is selected or "completed". I have been searching vigorously on here how to save the selected cell state to NSUserDefaults.

My ViewController:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var defaults = NSUserDefaults.standardUserDefaults()

@IBOutlet weak var toDoListTable: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    if defaults.objectForKey("toDoList") != nil {

        toDoList = defaults.objectForKey("toDoList") as [String]

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return toDoList.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    return cell

}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    selectedCell.contentView.alpha = 0.3



}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    var deselectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    deselectedCell.contentView.alpha = 1.0

}






func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == UITableViewCellEditingStyle.Delete {

        toDoList.removeAtIndex(indexPath.row)

        toDoListTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)

        defaults.setObject(toDoList, forKey: "toDoList")



    }

}


override func viewDidAppear(animated: Bool) {
    toDoListTable.reloadData()

}


}

I believe I'm just having an issue calling it/trying to figure out where to call it at. Any help would be greatly appreciated.

EDIT** What I have right now saves the content stored from a UITextField.

JohnJLilley
  • 173
  • 1
  • 2
  • 10
  • Could you provide more information? How do you retrieve the selection from User Defaults? Did you try to print out if the cell selection is actually saved? How do you set the cell selection once retrieved from the User Defaults? Actually, what's the issue? – Van Du Tran Mar 17 '15 at 19:44
  • You should use IF LET to unwrap your optional instead of doing it twice. if let toDoList = NSUserDefaults().stringArrayForKey("toDoList") as? [String] { ... } – Leo Dabus Mar 17 '15 at 20:31

3 Answers3

0

Is the problem that it doesn't save?

You can try calling synchronize to save it disk immediately to see if that makes a difference, although it shouldn't be required.

NSUserDefaults.standardUserDefaults().setObject(indexPath.row, forKey: "selection")
NSUserDefaults.standardUserDefaults().synchronize()
Community
  • 1
  • 1
Travis M.
  • 10,930
  • 1
  • 56
  • 72
0

NSIndexPath's row/item property is an NSInteger. This is not an object. Use setInteger(_:forKey:) instead. @Van Du Tan's answer for defaults.setValue(indexPath.row, forKey: "selection") may not work because you have to encapsulate the row inside an NSValue object.

i-konov
  • 842
  • 1
  • 7
  • 19
  • I don't think this is the issue. I tested this with both setObject and setValue while passing an NSInteger and it saves fine for both in Swift. – Travis M. Mar 17 '15 at 19:55
  • Hmm you are right I was thinking in obj-c collections context :) – i-konov Mar 17 '15 at 19:57
0

First off, I'm going to assume you have a line declaring NSUserDefaults:

let defaults = NSUserDefaults.standardUserDefaults()

I believe what your trying to do is store the alpha (assuming state) with respect to the row your selecting. In such a case

let key = "myTableViewWithRowOf\(indexpath.row)"
defaults.setFloat(selectedCell.contentView.alpha, forKey: key)

You can then retrieve this data by using the floatForKey method.

Ideally though you might want to end up with an NSArray associated with your tableView and store that using defaults.setObject(...). That way when your app loads you can call the whole array and loop through it restoring the "states".

Ryan Huebert
  • 613
  • 1
  • 6
  • 8