Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Alexander Hepburn
Alexander Hepburn
14,613 Points

How do you have an Activity Indicator show up before your table view loads?

^

Ryan Jin
Ryan Jin
15,337 Points

I think you have to drag the activity indicator first, and then the table view, but I am not sure since I haven't tried it yet

3 Answers

Ryan Jin
Ryan Jin
15,337 Points

You first need an outlet of the activity indicator connected in your code. Suppose the outlet of the activity indicator is called "activityInidicator" And the table view is called "tableView"

override func viewDidLoad() {
         super.viewDidLoad
         self.activityIndicator.hidden = false
         self.tableView.hidden = true
         self.activityIndicator.startAnimating();
}

At the place you want to show the table, add the following code

self.activityIndicator.hidden = true
self.tableView.hidden = false
self.activityIndicator.stopAnimating
self.tableView.reloadData();
Alexander Hepburn
Alexander Hepburn
14,613 Points

Thank you Ryan for your answer but, I know how to do that but for some reason I can't drag a Activity Monitor in to the TableView. Alexander

Chuck Toussieng
Chuck Toussieng
1,214 Points

Try dragging the Activity Indicator View onto the "View." On your Storyboard, there (should be) a View object with your Table View as a child. Drag the Activity Indicator in and make sure it's also a child of the main View.

If you place the Activity indicator as a child of the Table View, if you were to hide the table view, the Activity Indicator would hide along with it.

You can also save a line of code to hide and show the indicator by setting "Hides when stopped" in Interface Builder.

Alexander Hepburn
Alexander Hepburn
14,613 Points

Yeah it wont let me do that and there is no thing called view(its table view).

Chuck Toussieng
Chuck Toussieng
1,214 Points

Ah- so you're trying to add it into a ViewController that is a TableViewController -

OK- well, adding anything that is related to a UIView should not be added to the table view directly, but should be placed in a header or footer. read up on viewForHeaderInSection, viewForFooterInSection, etc.

Or I find the most flexible thing to do is NOT start off with a UITableViewController, but a basic UIViewController. Then modify the class file and add the TableView stuff:

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
... code
}

Add a TableView into that ViewController's View- along with whatever else you want to add in.

You could even add the Activity Indicator programmatically- something like:

let myIndicator:UIActivityIndicatorView = UIActivityIndicatorView (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
myIndicator.center = self.view.center
self.view.addSubview(myIndicator)
myIndicator.bringSubviewToFront(self.view)
myIndicator.startAnimating()

Sorry if this is rambling- or confusing- hopefully it helps.