wait for request ends before execute tableview init
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:
- you alloc and init your tableview AFTER the request did finished - this is not possible if you used IB to create your tableview
- you return
0
in yourUITableViewDelegate
s- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
method until the request is not finished. Then, reload the table and return the actual record count.
精彩评论