gpt4 book ai didi

iOS swift : Displaying asynchronous data on a TableView

转载 作者:可可西里 更新时间:2023-11-01 00:37:35 24 4
gpt4 key购买 nike

我有一个 ViewController,里面有一个 TableView,现在 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 异步几秒后获取数据,但它没有 ViewController 的引用来传递数据(最好是 ViewController.tableView.reloadData - 是我需要调用的)

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

最佳答案

好吧,我建议你使用回调,像这样添加一个输入

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

然后调用

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

关于iOS swift : Displaying asynchronous data on a TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30566920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com