Subview presentation delayed
In my UITableViewController subclass I want to prepare the tabular data and reload the table after the view has already appeared (the table therefore initially loads empty). In my app delegate I have methods to prod开发者_运维百科uce and remove an activity screen. My problem seems to be that the activity view presentation is being delayed until after the reloadData call has been made. I proved this by removing the hideActivity line and sure enough my activity view appeared simultaneously with the reloading of the table. Here's the viewDidAppear for my view controller...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}
I'm assuming this may have to do with the views not redrawing mid-method, but don't remember this happening before. Any ideas?
...although I know they work, here's my activity methods. Maybe the way I'm doing these allows for some sort of delayed appearance...
- (void)showActivity
{
self.activityView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.75f];
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((320.0f / 2.0f) - (37.0f / 2.0f), (480.0f / 2.0f) - (37.0f / 2.0f) - (20.0f / 2.0f), 37.0f, 37.0f)] autorelease];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[spinner startAnimating];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, spinner.frame.origin.y + spinner.frame.size.height + 10.0f, 320.0f, 30.0f)] autorelease];
label.textAlignment = UITextAlignmentCenter;
label.text = @"Working";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[self.activityView addSubview:label];
[self.activityView addSubview:spinner];
[self.window addSubview:self.activityView];
}
- (void)hideActivity
{
[self.activityView removeFromSuperview];
[self.activityView release];
}
Your UI isn't updating until you return control to the runloop. You need to either perform the preparation of the data in a background thread, or return control to the runloop to update the UI before starting the preparation, like this:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
[self performSelector:@selector(prepareData) withObject:nil afterDelay:0.0];
}
- (void)prepareData {
[self prepare];
[self.tableView reloadData];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}
精彩评论