2012-01-08 34 views
0

我剛剛安裝了與ios5的xcode 4.2.1,我嘗試使用json。json ios5在屏幕上沒有任何東西

這是我的代碼

-(void)start 

{ 
    responseData = [[NSMutableData data]retain]; 
    membreArray = [NSMutableArray array]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http:******.php"]]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [responseData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"error %@",error); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [connection release]; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    [responseData release]; 

    jsonArray = [responseString JSONValue]; 

    NSLog(@"array %@",jsonArray); 



} 
NSLog(@"array %@",jsonArray);我可以看到所有的記錄,一切正常

。但在我的桌面屏幕上沒有任何東西。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [jsonArray count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Configure the cell. 

    NSDictionary *dico = [self.jsonArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [dico objectForKey:@"name"]; 


    return cell; 
} 

上的Xcode 3此代碼的工作,但不是在Xcode 4

薩姆幫助請。

回答

0

當你完成加載數據後,你需要告訴你的tableview重新加載。我沒看到你的代碼。嘗試是這樣的:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [connection release]; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    [responseData release]; 

    jsonArray = [responseString JSONValue]; 

    NSLog(@"array %@",jsonArray); 
    /* This tells your tableView to reload */ 
    [tableView reloadData]; 
} 

此外,請確保您的TableView通過一個IBOutlet連接的設計師。否則你的命令將會充耳不聞。