2015-06-01 35 views
2

我有一個ViewController,其中TableView在裏面,現在TableView的數據在另一個類中異步地通過http調用拉取。iOS Swift:在TableView上顯示異步數據

class ViewController1: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    override func viewDidLoad() { 
      super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 
      self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

      MyHttpClass().getData()//INITIATING getting data asynchronously 
... 
} 

MyHttpClass異步幾秒鐘後得到的數據,但它不具備ViewController的參考交出的數據(理想情況下ViewController.tableView.reloadData - 是我需要調用)

有沒有更好的方法來處理這個問題?我如何獲得參考。到ViewController實例何時MyHttp獲取數據?任何更好的方法來處理這個?

回答

2

解決此問題的最佳方法是使MyHttpClass().getData()在完成異步加載數據時執行回調。在這個回調中,你可以重新加載tableview。

3

好吧,我建議你使用回撥,增加投入,因爲這

func getData(callBack:()->()){ 
    //When finished 
    callBack() 
} 

然後調用

MyHttpClass().getData() {() ->() in 
     self.tableView.reloadData() 
    }