Aris Guimerá
Aris Guimerá

Reputation: 1017

Reload UITableview after back

I have simple table on my View. Another view have array with some values. When I back, I want to reload table with new values, but dont work.

My class:

 let textCellIdentifier = "cell"
var menu:[[String]] = [[]]
var buscaEmp:BuscadorEmpresa = BuscadorEmpresa()


@IBOutlet weak var tablaVista: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()
    recuperaEmpresas()
    tablaVista.delegate = self
    tablaVista.dataSource = self

}

func recuperaEmpresas(){

      menu = buscaEmp.getEmpresas()
}

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return menu.count
}




func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

    let row = indexPath.row
    cell.nombreEmp.text = menu[row][0]
    return cell

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    recuperaEmpresas()
    tablaVista.reloadData()
}

I use viewWillAppear to reload table before reopen the view.

Upvotes: 3

Views: 2465

Answers (1)

David Ansermot
David Ansermot

Reputation: 6112

in viewWillAppear, you have to call recuperaEmpresas() and after it, call tableView.reloadData().

With this, it will refill the menu datasource with fresh data from recuperaEmpresas() and reload the table view with the new datasource values.

Edit :
And be sure that recuperaEmpresas() refresh data with new data, if it's always the same, you won't see any changes...

Upvotes: 2

Related Questions