开发者

wait for request ends before execute tableview init

开发者 https://www.devze.com 2023-04-12 22:35 出处:网络
following code uses ASIhttprequest to fill appData array. View also includes a tableview and when is launched, getData function is executed first but then I would like to wait until request ends and a

following code uses ASIhttprequest to fill appData array. View also includes a tableview and when is launched, getData function is executed first but then I would like to wait until request ends and appData is filled before execute tableView init methods. Now tableView methods are executed before request ends. How to do that? Thank you.

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            NSArray *datos = [[NSArray alloc] initWithObjects:(@"test"), nil];

            self.appData = datos;

            [datos release];

        }

        [self getData];

        return self;
    }

    - (void)getData
    {
        activityIndicator.hidden = NO;
        [activityIndicator startAnimating];

        开发者_Python百科NSURL *url = [NSURL URLWithString:@"http://test.com/iphone.php"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        NSLog(@"URL = %@",url);

        [request setValidatesSecureCertificate:NO];
        [request setRequestMethod:@"POST"];
        [request setPostValue:@"admin" forKey:@"key1"];
        [request setPostValue:@"admin" forKey:@"key1"];
        [request setDelegate:self];
        [request startAsynchronous];  

    }


    - (void)requestFinished:(ASIHTTPRequest *)request {
        NSLog(@"%@", [request responseString]);
        //NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]);
        [activityIndicator stopAnimating];
        activityIndicator.hidden = YES;

        appData = [[request responseString] componentsSeparatedByString: @"#"]; 

        int x =0;

        while (x<[appData count] - 1)
        {
            NSLog(@"cells = %@",[appData objectAtIndex: x]);
            x = x+1;
        }

    }


You have two options here:

  1. you alloc and init your tableview AFTER the request did finished - this is not possible if you used IB to create your tableview
  2. you return 0 in your UITableViewDelegates - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method until the request is not finished. Then, reload the table and return the actual record count.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号