0

問題が 1 つあります。

テーブルビューがあり、セルをクリックするとサーバーからデータがロードされます。これには時間がかかる可能性があるため、アクティビティ インジケーター ビューを表示したいと思います。

-(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

UI は通常、コードが制御を実行ループに戻すまで更新されません。getDataGLN:type:サーバーからデータを取得するまで、メソッドは返されません。したがって、サーバーからデータを取得するまで UI を更新することはできません。

そうしないでください。バックグラウンド スレッドにデータをロードし、メイン スレッドの制御をすぐに実行ループに戻します。Concurrency Programming GuideApple の開発者向けビデオで多くのヘルプを見つけることができます。

于 2013-06-26T07:56:01.343 に答える
0

setNeedsDisplay を呼び出すのはなぜですか?

とにかく、それを行う正しい方法は、読み込みインジケーターを表示したいときにすべての UI を作成しないことです。たとえばViewDidLoadで事前に行い、背景ビューを非表示にします。表示したい場合は、hidden プロパティを NO にします。

于 2013-06-26T07:51:39.520 に答える