子视图演示推迟

时间:2011-01-13 17:49:35

标签: objective-c cocoa cocoa-touch

在我的UITableViewController子类中,我想准备表格数据并在视图出现后重新加载表格(因此表格最初加载为空)。在我的app委托中,我有生成和删除活动屏幕的方法。我的问题似乎是活动视图演示被推迟到reloadData调用完成之后。我通过删除hideActivity行证明了这一点,并确保我的活动视图与重新加载表同时出现。这是我的视图控制器的viewDidAppear ...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showActivity];
    [self prepare];
    [self.tableView reloadData];
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hideActivity];
}

我认为这可能与不重绘中间方法的视图有关,但不记得以前发生过这种情况。有什么想法吗?

...虽然我知道它们有效,但这是我的活动方法。也许我正在做这些的方式允许某种延迟的外观......

- (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];
}

1 个答案:

答案 0 :(得分:0)

在将控制权返回到runloop之前,您的UI不会更新。您需要在后台线程中执行数据准备,或者在开始准备之前将控制权返回到runloop以更新UI,如下所示:

- (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];
}