IOS Swift: displaying asynchronous data in TableView

I have ViewControllerwith TableViewin it, now the data for is TableViewpulled through 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
...
}

MyHttpClassgets the data in a few seconds asynchronously, but it doesn’t have a link ViewControllerto transmit data (ideally, ViewController.tableView.reloadData is what I need to call)

Is there a better way to handle this? How do I get a link. to the ViewControllerinstance when it MyHttpreceives the data? Any better way to handle this?

+2
source share
2 answers

Well, I suggest you use a callback, add input like this

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

Then call

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

- MyHttpClass().getData() , . .

+2

All Articles