iOS Swift:在 TableView 上显示异步数据 [英] iOS Swift: Displaying asynchronous data on a TableView

查看:28
本文介绍了iOS Swift:在 TableView 上显示异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewController,其中有一个 TableView,现在 TableView 的数据是通过 http 调用异步拉取到另一个类中的.

I have a ViewController with a TableView in it, now the data for TableView is pulled via an http call asynchronously in another class.

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- 这是我需要打电话的)

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)

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

Is there a better way to handle this? How do i get ref. to ViewController instance when MyHttp gets the data? 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()
}

然后调用

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

这篇关于iOS Swift:在 TableView 上显示异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆