Home>
I want to ask

We are creating a notepad app now.
I can't express it well, but when I tap the cell from the home screen, go to the details screen, edit and save the text on the details screen, the number of cells has increased. (The photo is posted below.)

Code
// Home screen
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var memoTableView: UITableView!
    var memoArray = [String] ()
    let ud = UserDefaults.standard
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        return memoArray.count
    }

    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        let cell = tableView.dequeueReusableCell (withIdentifier: "memoCell", for: indexPath)
        cell.textLabel? .text = memoArray [indexPath.row]
        return cell
    }
    func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue (withIdentifier: "toDetail", sender: nil)
        // Press to release the pressed state
        tableView.deselectRow (at: indexPath, animated: true)
    }

    override func viewDidLoad () {
        super.viewDidLoad ()
        memoTableView.delegate = self
        memoTableView.dataSource = self
    }
    override func viewWillAppear (_ animated: Bool) {
        loadMemo ()
    }
    override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
        // Destination crash prevention
        if segue.identifier == "toDetail" {
            // get detailViewController
            // as! Downcast with DetailViewController
            let detailViewController = segue.destination as! DetailViewController
            // Can get the selected cell before transition
            let selectedIndexPath = memoTableView.indexPathForSelectedRow!
            detailViewController.selectedMemo = memoArray [selectedIndexPath.row]
            detailViewController.selectedRow = selectedIndexPath.row
        }
    }
    func loadMemo () {
        if ud.array (forKey: "memoArray")! = nil {
            // Get only when it is not nil because it is unwrapped with as!
            memoArray = ud.array (forKey: "memoArray") as! [String]
            // reloads mememoTableView.reloadData ()
        }
    }
    func tableView (_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // remove the indexPath row in the resultArray
            memoArray.remove (at: indexPath.row)
            // Save the deleted array in the app again
            ud.set (memoArray, forKey: "memoArray")
            // Update tableView
            tableView.reloadData ()
        }
    }
}
// Detail screen
import UIKit
class DetailViewController: UIViewController {
    @IBOutlet weak var memoTextView: UITextView!
    var selectedRow: Int!
    var selectedMemo: String!
    override func viewDidLoad () {
        super.viewDidLoad ()
        memoTextView.text = selectedMemo
        let kbToolBar = UIToolbar (frame: CGRect (x: 0, y: 0, width: 320, height: 40))
        kbToolBar.barStyle = UIBarStyle.default // set style
        kbToolBar.sizeToFit () // change the size to fit the screen width
        // spacer
        let spacer = UIBarButtonItem (barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
        // Close button
        let commitButton = UIBarButtonItem (barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector (self.commitButtonTapped))
        kbToolBar.items = [spacer, commitButton]
        memoTextView.inputAccessoryView = kbToolBar
    }
    @objc func commitButtonTapped () {
        self.view.endEditing (true)
    }
    // Button to tap when changing screen
    @IBAction func save (_ sender: Any) {
        let inputText = memoTextView.text
        let ud = UserDefaults.standard
        if ud.array (forKey: "memoArray")! = nil {
            // get to saveMemoArray
            var saveMemoArray = ud.array (forKey: "memoArray") as! [String]
                // Is something written in the text?
            if inputText! = "" {
                // Add to array
                saveMemoArray.append (inputText!)
                ud.set (saveMemoArray, forKey: "memoArray")
            } else {
                showAlert (title: "Nothing entered")}
        } else {
            // If nothing is written at first
            var newMemoArray = [String] ()
            // Nil forced unwrapping will give an error
            if inputText! = "" {
                // Forced unwrapping because inputtext is optional
                newMemoArray.append (inputText!)
                ud.set (newMemoArray, forKey: "memoArray")
            } else {
                showAlert (title: "Nothing entered")
            }
        }
        showAlert (title: "Save complete")
        ud.synchronize ()
    }
    func showAlert (title: String) {
        let alert = UIAlertController (title: title, message: nil, preferredStyle: .alert)
        alert.addAction (UIAlertAction (title: "OK", style: .default, handler: nil))
        alert.addAction (UIAlertAction (title: "Cancel", style: .cancel, handler: nil))
        self.present (alert, animated: true, completion: nil)
    }
    // Delete button
    @IBAction func deleteMemo (_ sender: Any) {
        let ud = UserDefaults.standard
        if ud.array (forKey: "memoArray")! = nil {
            var saveMemoArray = ud.array (forKey: "memoArray") as! [String]
            saveMemoArray.remove (at: selectedRow)
            ud.set (saveMemoArray, forKey: "memoArray")
            ud.synchronize ()
            //Screen transition
            self.navigationController? .popViewController (animated: true)
        }
    }
    // Share button
    @IBAction func showActivityView (_ sender: UIBarButtonItem) {
        let activitycontroller = UIActivityViewController (activityItems: [memoTextView],

 applicationActivities: nil)
        self.present (activitycontroller, animated: true, completion: nil)
    }
}
Image



I'm really sorry for lack of vocabulary.
A little hurry. Thank you.