以下代码使用ASIhttprequest来填充appData数组。 View还包括一个tableview,当启动时,首先执行getData函数但是我想等到请求结束并且在执行tableView init方法之前填充appData。现在tableView方法在请求结束之前执行。怎么做?谢谢。
- (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];
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;
}
}
答案 0 :(得分:0)
这里有两个选项:
0
UITableViewDelegate
方法中返回- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
,直到请求未完成为止。然后,重新加载表并返回实际记录计数。