Swift - How to create a table view based on data loaded asynchronously?

My app downloads json from the internet, draws a map with icons and presents the data in a table.

The map is not a problem because I am writing my custom function to draw the map and call it right after the dispatch_async (dispatch_get_main_queue () line, which works great - the map is waiting for the data to be loaded. However, the problem occurs when I want to do the same most with a table view.

My code is quite long, so I'm only going to show short snippets to get the idea of ​​the problem.

I've tried the following modification:

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
                // Do any additional setup after loading the view, typically from a nib.
                self.tableView.backgroundColor = UIColor.clearColor();
                self.tableView.delegate = self;
                self.tableView.dataSource = self;

      

But unfortunately that doesn't help in any way - the table view is loaded again before the data is loaded, resulting in an empty table.

The table view uses its own predefined functions such as:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

      

And placing them inside an asynchronous task is impossible without errors

My question is, how to deal with things like this (I mean a situation where you want an element such as a table view to "wait" for the data to be loaded first, and you cannot put it in a loadable task)?

I hope I was able to present the problem relatively clearly, thanks in advance

+3


source to share


1 answer


The table view works through a delegate and a data source. By setting these properties, you tell the table view where to report events and where to look for its data.

Since these methods are called "by" the table view (or at least posted by the UI thread), you don't run them directly. Instead, you set up a delegate and data source (as you did), and then call methods on the table view itself to indicate when to check for new data.

In particular, you should look at this section of the documentation . You can get away with asynchronous dispatch self.tableView.reloadData()

(if the rest of your application is aware of the new data in the callback methods you found).




The basic idea is that yours UITableViewDataSource

knows how to populate a table view with information from some data structure (for example, you can have an array as a property).

When the underlying data changes (for example, due to the fact that you downloaded something from the Internet), you change this data structure (for example, replace an array) and tell the corresponding parts of the table view to be reloaded.

Then various methods will be called UITableViewDataSource

that will repopulate the table view with new data.

+7


source







All Articles