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
.