iOS Swift:在TableView上显示asynchronous数据

我有一个ViewController和一个TableView ,现在TableView的数据是通过一个http调用asynchronous在另一个类拉。

 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在几秒钟后asynchronous获取数据,但是它没有引用ViewControllerMyHttpClass数据(理想情况下,ViewController.tableView.reloadData是我需要调用的)

有没有更好的方法来处理这个问题? 我如何得到参考。 当MyHttp获取数据的ViewController实例? 任何更好的方法来处理这个?

那么,我build议你使用回拨,添加一个input为此

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

然后打电话

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

解决这个问题的最好方法是让MyHttpClass().getData()在完成asynchronous加载数据时执行callback。 在这个callback中,你可以重新加载tableview。