Save/load 自动将行添加到表视图

Save/load added rows to tableview automatically

也许一些 "Pro" 可以帮助我解决我的问题。 找了很久都没找到合适的答案

我是 swift 的新人。我修补我的第一个项目.....当然是一个待办事项列表。 所以我的问题是,如何保存和加载添加到 tableview 的所有行 自动(没有 "save Buton")。当我重新启动应用程序时,所有数据都丢失了。

感谢您的帮助。

来自德国的问候

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

}

你没有说你用什么来持久存储你的 ToDo 列表,我在你的代码中没有看到它。这是 Reddit 上关于使用 NSUserDefaults 的一个很好的解释,并在 plists 上进行了相关讨论。

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