将添加的行自动保存/加载到tableview

时间:2015-06-30 18:12:27

标签: swift xcode6

也许一些“专业”可以帮助我解决我的问题。 我搜索了很长时间,但我没有找到一个好的答案。

我是斯威夫特的新人。我修补了我的第一个项目.....当然是ToDo List。 所以我的问题是,如何保存和加载添加到tableview的所有行 自动(没有“保存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)
    }
}

}

1 个答案:

答案 0 :(得分:2)

您没有说出您用来持久存储待办事项列表的内容,而我在您的代码中看不到它。这是一个很好的Reddit解释,使用NSUserDefaults进行有关plists的相关讨论。

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