felix_xiao
felix_xiao

Reputation: 1628

Swift UITableViewController Reduce Loading Time

I'm currently creating an iOS app in Swift 1.2 with a master-detail UITableViewController setup so that MasterTableViewController has cells that push to DetailTableViewController.

DetailTableViewController has fairly complex cells that pull data from a web client to generate a chart, similar to in Apple's Health app. It usually takes about 5-10 seconds for all the data to download.

As a result, there is a 5-10 second delay when a cell in MasterTableViewController is tapped before DetailTableViewController is shown.

I would ideally like to push immediately from the MasterTableViewController to the DetailTableViewController and then display an activity indicator on the DetailTableViewController page that is dismissed when all the data is downloaded.

Could someone point me in a good direction to accomplish this? Thank you! :)

My code is below for pushing from master to detail. Fairly basic. I feel like if I change something in the viewDidLoad or cellForRowAtIndexPath method, I could get this done fairly easily.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    let detailVC = DetailTableViewController()
    detailVC.navigationItem.title = "Your Charts"
    self.navigationController?.pushViewController(detailVC, animated: true)
}

Upvotes: 0

Views: 183

Answers (1)

Fred Faust
Fred Faust

Reputation: 6790

On your detail view controller you can create a UIActivityIndicatorView variable.

In viewDidAppear you can call the activity indicator, bring it to front and start animating it.

The create a function that populates an array that will store the data your cells will use and re work your cellForRowAtIndexPath to use it. (Instead of fetching the data in each cell).

Then in your function to fetch data, right after (or before) you update your table tell the activity indicator to stop animating.

Upvotes: 2

Related Questions