2011-10-13 25 views
0

以下代碼使用ASIhttprequest填充appData數組。視圖還包括一個tableview,當啓動時,getData函數首先被執行,然後我想等到請求結束並在執行tableView init方法之前填充appData。現在,tableView方法在請求結束之前執行。怎麼做?謝謝。等待請求結束後執行tableview init

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

你這裏有兩種選擇:

  1. 你alloc和初始化你的tableview後請求沒有完成 - 如果你使用IB來創建你的tableview
  2. 您在返回0這是不可能的您的UITableViewDelegate s - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法,直到請求未完成。然後,重新加載表並返回實際記錄數。