Frage

I have one problem.

I have table view and when I click on cell I load data from server. Because this could take some time I want to show activity indicator view.

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

This works fine, but when I put this in

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

I see that UIActivityIndicatorView is shown after it get data from server. How to force main view to update immediately?

War es hilfreich?

Lösung 2

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.

Andere Tipps

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

Why do you call setNeedsDisplay?

Anyway, The right way to do it, is not creating all the UI when you want to show loading indicator. Do it in advance - in ViewDidLoad for example, and just hide the background view. When you want to show it, just turn it's hidden property to NO.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top