Jhuanderson Macias
Jhuanderson Macias

Reputation: 5

ios Reloading TableView

I am editing the Email on the second viewController. The Label.text(in ViewController) does change if i modify the email address in the second view(tableViewBudget), but the table view Cell does not change. It always shows the first value of savedEmail.text. How can I modify my code so that tableView cell will change when i change the string in the other table View? Thank You!

import UIKit

class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var savedEmail: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
     }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func exitToHere ( sender: UIStoryboardSegue){
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        cell.textLabel?.text = savedEmail.text
        return cell
    }
}

Second View Controller

import UIKit

 class TableviewBudget: UIViewController {

    @IBOutlet weak var emailField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }   
    @IBAction func updateEmail(_ sender: Any) {
        (presentingViewController as! ViewController).savedEmail.text =
            self.emailField.text
    }

    @IBAction func hideKeyboard(_ sender: Any) {
        self.emailField.resignFirstResponder()
    }
    override func viewWillAppear(_ animated: Bool) {
        self.emailField.text = (presentingViewController as! ViewController).savedEmail.text

        super.viewWillAppear(animated)
    }

}

Upvotes: 0

Views: 1380

Answers (3)

Artem
Artem

Reputation: 401

try to call

 tableView.reloadData() 

or

 tableView.beginUpdates()
 tableView.reloadRows(at: [yourIndexPath], with: .fade)
 tableView.endUpdates()

Second version is better when you have to much rows but you want to reload only one or few of them

Upvotes: 0

Rikh
Rikh

Reputation: 4232

You can use delegates to send a callback to the first UIViewController, in your case class ViewController as soon as user is done editing the text.

In your class TableViewBudget change it to like so, this will create a delegate to pass data to a previous view controller

For Swift 3

protocol delegateVC{

    func updateEmail(email : String)
}

class TableviewBudget: UIViewController{
    //add this inside this class
    var delegate : delegateVC?
    ...
}

Now change your function to

@IBAction func updateEmail(_ sender: Any) {
    self.delegate?.updateEmail(email: self.emailField.text)
}

Now in your class ViewController add-

class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate, delegateVC{

     func updateEmail(email : String){
         //update label here inside your first VC by getting the cell, using the indexPath of the cell that contains the E-mail
         //also update your modal here to contain the next email otherwise if cellForRow gets called again, the e-mail field will get reset
         cell.textLabel?.text = email
     }
}

Also remember to set this delegate in your prepareForSegue while you push TableViewBudget from ViewController, something like

let tableViewBudget = TableViewBudget()
tableViewBudget.delegate = self

Upvotes: 1

Matias Elorriaga
Matias Elorriaga

Reputation: 9150

in ViewController:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reload()
}

Upvotes: 0

Related Questions