0

When I run tableView.reloadData() in UITableViewController, it works, but when I call a function to update this tableview from another controller it does not work.

//This way works normally

func reloadTableView() {

    let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true ).first as String
    let db = Database("\(path)/db.sqlite");

    conversasArray.removeAll(keepCapacity: true);

    let stmt = db.prepare("SELECT ZNOME,ZUlTIMAMSG,ZIDMENSAGEM,ZIMAGE FROM ZUSUARIO,ZCHAT WHERE ZTELEFONE = ZTO")
    var objetoConversa : AnyObject = [];

    for row in stmt {
        objetoConversa = ["ZNOME":"\(row[0]!)", "ZUlTIMAMSG":"\(row[1]!)", "ZIDMENSAGEM":"\(row[2]!)", "ZIMAGE":"\(row[3]!)"];
        //Adiciona o objeto dentro do array de objetos.
        conversasArray.append(objetoConversa)
    }
    tableView.reloadData();

}

//But when I do it another file tableView.reloadData() does not work.

var a = TableViewController();

a.reloadTableView();

  • 1
    Because `var a = TableViewController()` creates a new and completely unrelated table view controller instance ... What did you think it does? – Martin R Mar 28 '15 at 15:59
  • Check this question/answer: http://stackoverflow.com/questions/25921623/how-to-reload-tableview-from-another-view-controller-in-swift – Shades Mar 28 '15 at 16:27
  • I thought it would bring the current instance of TableViewController (), I'm new and I'm still learning to program in SWIFT, thank you for the solution. – Rodrigo Silva Mar 30 '15 at 12:31

1 Answers1

0

Use save instance of tableview while reload tableview.

And also Reload tableview in main thread.

extension UITableView {

/// Reload TableView in main thread

func reloadInMainThread() {

    DispatchQueue.main.async {

        self.reloadData()

    }
}

}

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44