0
votes

I am a newbie and learning ios programming. I have made a custom table view cell. I am trying to append data in it via user defaults but its not storing the data, every time i go back the app refreshes making the table view blank screen.

// data sent from here


class ViewController: UIViewController {

    @IBOutlet weak var textfield: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func submit(_ sender: AnyObject) {



        performSegue(withIdentifier: "move", sender: nil)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination as? secondviewcontroller



        destination?.list.append(textfield.text!)

        let defaults = UserDefaults.standard
        defaults.set(destination?.list, forKey: "listarray")
        UserDefaults.standard.synchronize()

    }
}


// here i want to retreive
class secondviewcontroller: UIViewController {


    @IBOutlet weak var tblView: UITableView!
    var list = ["fahad","Ali","tahir"]

   var abc = ""

override func viewDidLoad() {
    super.viewDidLoad()
    tblView.dataSource  = self;
    tblView.delegate = self;


    let storedarray = UserDefaults.standard.object(forKey: "listarray")

    print("hi\(storedarray)")
}



override func viewDidAppear(_ animated: Bool) {
     tblView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    tblView.reloadData()
}

@IBAction func backbtn(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

import UIKit

class secondviewcontroller: UIViewController {

@IBOutlet weak var tblView: UITableView!
var list = ["fahad","Ali","tahir"]

var abc = ""

override func viewDidLoad() {
    super.viewDidLoad()
    tblView.dataSource  = self;
    tblView.delegate = self;



}



override func viewDidAppear(_ animated: Bool) {
     tblView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
    let storedarray = UserDefaults.standard.object(forKey: "listarray")

    print("hi\(storedarray)")
    tblView.reloadData()
}

@IBAction func backbtn(_ sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

extension secondviewcontroller : UITableViewDataSource , UITableViewDelegate{

func numberOfSections(in tableView: UITableView) -> Int {

    return 1;
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print (list)
    return list.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell;
    cell.textlabel.text = list[indexPath.row];


    return cell;


}

}

// my custom table view
import UIKit

class   TableViewCell: UITableViewCell {

    @IBOutlet weak var textlabel: UILabel!   
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

My UI storyboardgetting this error

2

2 Answers

0
votes
  1. you must subclass the UITableViewDelegate and UITableViewDataSource methods and implement the required methods for it to display the data in your custom view. refer to this: http://clean-swift.com/refactoring-table-view-data-source-and-delegate-methods/
    1. try adding the code for stored array into the viewwillappear method..
0
votes

These lines recreates the same list with an new element without considering the saved items:

    destination?.list.append(textfield.text!)

    let defaults = UserDefaults.standard
    defaults.set(destination?.list, forKey: "listarray") 
    UserDefaults.standard.synchronize()

You should instead:

let newEntry = textfield.text!
let defaults = UserDefaults.standard
if destination?.list.count == 3 {  //3 because you hardcoded 3 items in next controller
      destination?.list.append(newEntry) 
} else {
      let obj = defaults.object(forKey: "listarray") as? [String] ?? [String]()
      destination?.list = obj.append(newEntry)
}
defaults.set(destination?.list, forKey: "listarray")
UserDefaults.standard.synchronize()