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获取数据,但是它没有引用ViewController
来MyHttpClass
数据(理想情况下,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。