0

我有一个问题。

我有表格视图,当我单击单元格时,我从服务器加载数据。因为这可能需要一些时间,所以我想显示活动指示器视图。

-(void)startSpiner{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIView * background = [[UIView alloc]initWithFrame:screenRect];
    background.backgroundColor = [UIColor blackColor];
    background.alpha = 0.7;
    background.tag = 1000;

    UIActivityIndicatorView * spiner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spiner.frame = CGRectMake(0, 0, 50, 50);
    [spiner setCenter:background.center];
    [spiner startAnimating];
    [background addSubview:spiner];
    [background setNeedsDisplay];
    [self.view addSubview:background];
}

这很好用,但是当我把它放进去时

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self startSpiner];
    Server *server = [[Server alloc]init];
    self.allItems = [server getDataGLN:self.object.gln type:1];
}

我看到 UIActivityIndi​​catorView 在从服务器获取数据后显示。如何强制主视图立即更新?

4

3 回答 3

2
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self startSpiner];

     ////If your server object is performing some network handling task. dispatch a
     ////background task.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        Server *server = [[Server alloc]init];

        self.allItems = [server getDataGLN:self.object.gln type:1];
    });
}
于 2013-06-26T07:50:45.000 回答
0

为什么叫 setNeedsDisplay?

无论如何,正确的做法是,当您想要显示加载指示器时,不要创建所有 UI。提前做——例如在 ViewDidLoad 中,然后隐藏背景视图。当你想显示它时,只需将它的隐藏属性设置为 NO。

于 2013-06-26T07:51:39.520 回答
0

The UI normally isn't updated until your code returns control to the run loop. Your getDataGLN:type: method isn't returning until it gets the data from the server. Thus the UI cannot be updated until you've got the data from the server.

Don't do that. Load your data on a background thread and return control of the main thread to the run loop immediately. You will find lots of help in the Concurrency Programming Guide and in Apple's developer videos.

于 2013-06-26T07:56:01.343 回答