Save Uitableview data in swift 3

This code is a notepad but it cant save its entries. When you switch pages all of the text is erased. I tried to use userdefualts to save my text but I could not figure it out. My button is saveTheTable. Here is picture of my app.

    import UIKit

    class ViewControllerX: UIViewController, UITableViewDataSource{

    @IBOutlet weak var inter: UIWebView!
    var items: [String] = [""]

    @IBOutlet weak var listTableViewXX: UITableView!
    @IBAction func addX(_ sender: Any) {

    override func viewDidLoad() {
          listTableViewXX.dataSource = self

    let userDefaults = Foundation.UserDefaults.standard
    userDefaults.set( "String", forKey: "Key")


    var path = ""
    path = Bundle.main.path(forResource: "command", ofType: "pdf")!
    let url = URL(fileURLWithPath: path)
    self.inter.loadRequest(URLRequest(url: url))

@IBAction func mamssmas(_ sender: Any) {
    if let url = URL(string: "") {

@IBAction func savethetable(_ sender: Any) {

    //To retrieve from the key
    let userDefaults = Foundation.UserDefaults.standard
    let value  = userDefaults.string(forKey: "Key")

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "listItemX" ) as! ItemTableViewCellX
    cell.itemLabelX.text = items[indexPath.row]
    return cell

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
    func alert(){
    let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)
        (textfield) in
        textfield.placeholder = "Enter you sin"
    let add = UIAlertAction(title: "Add", style: .default){
        (action) in
        let textfield = alert.textFields![0]
    let cancel = UIAlertAction(title: "Cancel", style: .cancel) {
        (alert) in

    present(alert, animated: true, completion: nil)
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    items.remove(at: (indexPath as NSIndexPath).row)
    tableView.deleteRows(at: [indexPath], with: .automatic)

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


There are two main issues that I can see.

  1. Your table view is being populated with strings from self.items. I did not see anywhere in the posted code where you do something like: self.items = NSUserDefaults.standard.array(forKey: key). self.items is only ever appended to when adding from a textfield. Even if data was being saved properly to UserDefaults, it still wouldn’t populate properly.

  2. You are trying to save a UITableView to UserDefaults.
    let myText = listTableViewXX
    UserDefaults.standard.set(myText, forKey: “savedStringKey”)

    UserDefaults can only store basic/primitive data types. In this case, it would probably be better to save self.items.

Hopefully this gets you on the right track

Do I put self.items = NSUserDefaults.standard.array(forKey: key) in the save button?

The main issue you are experiencing is the data in the table gets cleared out when you navigate away and return to the view. You need to ensure self.items is populated BEFORE your table view needs to display it.