[英]iOS Swift: Displaying asynchronous data on a TableView
I have a ViewController
with a TableView
in it, now the data for TableView
is pulled via an http call asynchronously in another class. 我有一个带有
TableView
的ViewController
,现在TableView
的数据是通过另一个类中的http调用异步提取的。
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
gets the data after a few seconds asynchronously, but it does not have a reference of the ViewController
to hand over the data (ideally the ViewController.tableView.reloadData - is what i need to call) MyHttpClass
几秒钟后异步获取数据,但是它没有ViewController
的引用来移交数据(理想情况下,我需要调用ViewController.tableView.reloadData)
Is there a better way to handle this? 有没有更好的方法来解决这个问题? How do i get ref.
我如何获得参考。 to
ViewController
instance when MyHttp
gets the data? MyHttp
获取数据时, MyHttp
将其发送到ViewController
实例? Any better way to handle this? 还有更好的方法来解决这个问题吗?
Well, I suggest you to use call back,add a input as this 好吧,我建议您使用回叫,为此添加一个输入
func getData(callBack:()->()){
//When finished
callBack()
}
Then call 然后打电话
MyHttpClass().getData() { () -> () in
self.tableView.reloadData()
}
The best way to solve this problem would be to make MyHttpClass().getData()
take a callback to execute when it finishes loading the data asynchronously. 解决此问题的最佳方法是使
MyHttpClass().getData()
在完成异步加载数据后执行回调。 In this callback you can then reload the tableview. 然后,您可以在此回调中重新加载tableview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.