0

I am trying add data dynamically to a new cell every time the unwindsegue method is triggered. So far it's only creating one cell. I cant't figure out what i am doing wrong. Iv'e tried implementeing a delegate and data source, but i get a redundant error. FYI: I am using Storyboard.

View Controller 2

class NewPasswordViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

var passName:String?
var passWord:String?


//var passWordDataArray:[Password]

var NewPassWord = Password()
    


@IBAction func getPassWordName(_ sender: UITextField) {
    passName = sender.text
    if passName != nil{
        NewPassWord.name = passName!
    }
}
@IBAction func getPassWord(_ sender: UITextField) {
    passWord = sender.text
    if passWord != nil{
        NewPassWord.password = passWord!
    }
}


@IBAction func saveNewPassWord(_ sender: UIButton) {
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {
    
    let vc = segue.destination as! PassWordTableViewController
    
    vc.passwords.append(NewPassWord)
    
 }

}

View Controller 1

class PassWordTableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    var passwords = [Password]()

    override func numberOfSections(in tableView: UITableView) -> Int {
        return passwords.count
    }

    // return the number of rows for the table
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return passwords.count
    }
    // Provide a cell for each row
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // Fetch a cell of the appropriate type.
        let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath) as! PassWordViewCell
        
        let thePassWord = passwords[indexPath.row]

        //Configure the cell's contents.
        cell.passWordName?.text = thePassWord.name
        
        return cell
    }
    
    @IBAction func unWindToPassWordTable(unWindSegue: UIStoryboardSegue){
        
    }
    
    }
Creatif_IV
  • 597
  • 6
  • 16

1 Answers1

1

The reason you see only one cell in your tableView is because whenever you perform segue you create a new instance of PassWordTableViewController, with an empty array of Password.

You have to use the unwind function to pass back the data from ViewControlletr1 to ViewController2, then append the new data and pass it together with your segue.

Click here to see an implementation example.