Home>

I asked the following questions before.

"[Swift, Xcode] I want to delete the Todo app task by swiping"
https://www.tutorialfor.com/go.php?id=143780

Thanks to the question above, I was able to delete the task with a swipe.

However, when the task was deleted and the Todo app was restarted, it returned to the state where it was not deleted.

For example, if you delete the task "App", turn off the app window, and start the app again, the "App" task remains.

For me, once I delete it, I want it to be in a non-existing state the next time I start

Please give me an answer.

import UIKit
// array to store variables
var cell_array = [String] ()
class AddController: UIViewController {
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var textField: UITextField!
    @IBAction func addButton (_ sender: Any) {
        // Add the entered text
        cell_array.append (textField.text!)
        // Delete the textField value
        textField.text = ""
        // Register to UD
        UserDefaults.standard.set (cell_array, forKey: "TodoList")
    }

}
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        // return the number of cells
        return cell_array.count
    }
    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        // Create a cell variable
        let cell: UITableViewCell = tableView.dequeueReusableCell (withIdentifier: "TodoCell", for: indexPath)
        // Store the contents of the cell registered in UD
        cell.textLabel! .text = cell_array [indexPath.row]
        // return the cell to display
        return cell
    }
    // Delete the task
    func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let index = indexPath.row
        cell_array.remove (at: index)
        UserDefaults.standard.synchronize ()
        tableView.reloadData ()
    }
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
        // Receive contents stored in UD
        if UserDefaults.standard.object (forKey: "TodoList")! = nil {
            cell_array = UserDefaults.standard.object (forKey: "TodoList") as! [String]
        }
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }


}
  • Answer # 1

    Savecell_arrayin UserDefaults when you delete a cell.

  • Answer # 2

    Did you read "aaa", "Mazinger", "App" from the source code when starting the app? ↓ Such a description.

    var cell_array = [String] ()
    cell_array = ["aaa", "Mazinger", "App"]


    To save thestate, please go to "Permanent Data".
    I overlooked and corrected it.
    As Mr. Fuzzbal said, the task is where the cell was deleted? Save the sequence as you did when you registered.