hanumanDev
hanumanDev

Reputation: 6614

How would I go about running some code while the tableView is loading?

How would I go about running some code while the tableView is loading. This is what I thought might work:

while ([self.tableView reloadData]) {
    do something
}

I would like to have a UIActivityIndicator run while the table loads and then disappear after the data has loaded.

thanks for any help

Upvotes: 0

Views: 116

Answers (2)

Darko Pavlovic
Darko Pavlovic

Reputation: 218

not completely sure, but it looks like you could do:

activityIndicator.hidesWhenStopped = true;
[activityIndicator startAnimating];

[tableView reloadData];

[self performSelector:@selector(updateFinished) withObject:nil afterDelay:0];

where updateFinished just calls

[activityIndicator stopAnimating].

If I am not wrong, you are doing everything on main thread. Every thread has it's run loop where user's methods, animations, input handling, timers and similar things are executed in a loop. At the point you are executing some user's code, calling reloadData schedules tables animations to be executed after the method is ended, and than by using performSelector with delay 0 you are scheduling activity indicator animation to be ended.

Upvotes: 1

Jonathan Arbogast
Jonathan Arbogast

Reputation: 9650

If you just want to have an activity indicator run while the table is loading:

[indicator startAnimating];
[self.tableView reloadData];
[indicator stopAnimating];

Thats assuming you have a UIActivityIndicator called indicator and its hidesWhenStopped property is set to YES.

If you want to do some other sort of work you will most likely need to do that work in a separate thread, dispatch queue, etc.

Upvotes: 2

Related Questions