Andreas
Andreas

Reputation: 589

Save/load added rows to tableview automatically

maybe some "Pro" can help me with my problems. I search for a long time, but i didn`t found a good answer.

I`m new in swift. I tinker my first project.....of course a ToDo List. So my question is, how can I save and load all rows that added to tableview automatically(without a "save Buton"). When I restart the app, all data are lost.

Thanks for helping.

Greetings from Germany

import UIKit

class ListTableViewController: UITableViewController {

var ToDo = [String]()

var newToDo: String = ""

@IBAction func cancel(segue:UIStoryboardSegue) {

}

@IBAction func done(segue:UIStoryboardSegue) {

    var DetailVC = segue.sourceViewController as! DetailViewController
    newToDo = DetailVC.name


    ToDo.append(newToDo)
    self.tableView.reloadData()


}


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.editing = true


    tableView.backgroundColor = UIColor.whiteColor()


    tableView.separatorColor = UIColor.orangeColor()

    //tableView.tableFooterView = UIView(frame:CGRectZero)


}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           // Return the number of rows in the section.
    return ToDo.count
}


override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let movedObject = self.ToDo[sourceIndexPath.row]
    ToDo.removeAtIndex(sourceIndexPath.row)
    ToDo.insert(movedObject, atIndex: destinationIndexPath.row)
    NSLog("%@", "\(sourceIndexPath.row) => \(destinationIndexPath.row) \(ToDo)")
    // To check for correctness enable: self.tableView.reloadData()


}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...


    cell.textLabel!.text = ToDo[indexPath.row]


    //cell.textLabel?.textColor = UIColor.redColor()


    cell.backgroundColor = UIColor.clearColor()

    return cell


}






override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        ToDo.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath],
            withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

}

import UIKit

class ListTableViewController: UITableViewController {

var ToDo = [String]()

var newToDo: String = ""

@IBAction func cancel(segue:UIStoryboardSegue) {

}

@IBAction func done(segue:UIStoryboardSegue) {

    var DetailVC = segue.sourceViewController as! DetailViewController
    newToDo = DetailVC.name


    ToDo.append(newToDo)
    self.tableView.reloadData()


}


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.editing = true


    tableView.backgroundColor = UIColor.whiteColor()


    tableView.separatorColor = UIColor.orangeColor()

    //tableView.tableFooterView = UIView(frame:CGRectZero)


}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           // Return the number of rows in the section.
    return ToDo.count
}


override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let movedObject = self.ToDo[sourceIndexPath.row]
    ToDo.removeAtIndex(sourceIndexPath.row)
    ToDo.insert(movedObject, atIndex: destinationIndexPath.row)
    NSLog("%@", "\(sourceIndexPath.row) => \(destinationIndexPath.row) \(ToDo)")
    // To check for correctness enable: self.tableView.reloadData()


}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...


    cell.textLabel!.text = ToDo[indexPath.row]


    //cell.textLabel?.textColor = UIColor.redColor()


    cell.backgroundColor = UIColor.clearColor()

    return cell


}






override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        ToDo.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath],
            withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

}

Upvotes: 1

Views: 114

Answers (1)

lnq
lnq

Reputation: 36

You didn't say what you are using to persistently store your ToDo list and I don't see it in your code. Here is a nice Reddit explanation of using NSUserDefaults to do so with a related discussion on plists.

http://www.reddit.com/r/swift/comments/28sp3z/whats_the_best_way_to_achieve_persistence_in/

Upvotes: 2

Related Questions