Reputation: 151
I have a table view cell , during click it , another table view will open , you can select something as the value of the table view cell. The issue here is the data in the second table view is big and it will take long time to load. So after I click the cell , the screen will froze there which is not user friendly. I want to displaying a progress bar during load the second table view. But I can not find a good place to add that. I am wondering in which method should I add the code to display the progress bar.
Upvotes: 2
Views: 2537
Reputation: 10005
What about displaying a UIActivityIndicator (sample image here) in the UITableViewCell accessory view?
You can place it with some code...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
}
You might also do you long-taking calculation in a background thread by calling
[self performSelectorInBackground:@selector(yourLongTakingMethod) withObject:nil];
Upvotes: 2
Reputation: 5819
If your app freezes, it means you are blocking the UIThread, like @stavash suggested. The progress bar is just an animated image, it won't solve your problem. What you want to do is to put your "data preparing code" into a thread. The easy way would be to use Grand Central Dispatch. To get started, visit this tutorial. You can skip the first half and focus on the actual thread blocks. The APIs are dispatch_queue_create
, dispatch_async
and dispatch_release
.
This will not make a progress bar. However, it'll unblock your UI. Then you can consider methods to make a progress bar. Use a builtin one, or some custom animation. Or even preload the data at the first tableview via a background task.
Upvotes: 0
Reputation: 14304
Two things:
I would seriously consider a different option rather than a progress bar, try for instance something like MBProgressHUD, it looks better and it suits your purpose.
For implementing this, you would have to make sure your UI thread isn't stuck (and therefore the application doesn't "freeze") - this means calling the loading process on a background thread. The method that starts the loading process should start the progress indicator and the method that deals with displaying the loaded data (once it's ready) should remove it. Good luck!
Upvotes: 1