In my Swift app, I have a tableview, in which selecting a cell, takes you to a detail page, and the detail page takes time to load. I am trying to show an activity indicator as it loads. How can I do this? Right now, when a cell is selected, the activity indicator does not show.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
myActivityIndicator.center = view.center
myActivityIndicator.startAnimating()
view.addSubview(myActivityIndicator)
let defaults = NSUserDefaults.standardUserDefaults()
if let data_id = self.ids[indexPath.row] as? Int {
defaults.setValue("\(data_id)", forKey: "data_id")
defaults.synchronize()
self.performSegueWithIdentifier("showInfo", sender: self)
}
}
In your detailViewController
do the following
Add an activityIndicator
var activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0, 50, 50)) as UIActivityIndicatorView
activityIndicator.center = CGPoint(x: (self.view.frame.width)/2, y: (self.view.frame.height)/3)
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
activityIndicator.color = UIColor.grayColor()
self.view.addSubview(activityIndicator)
In your viewDidLoad
start the animation
activityIndicator.startAnimating()
Now you will for sure load some data in some function or what you do, when all this is done, stop the animation in the last function you call
activityIndicator.stopAnimating()