json-ios5在屏幕上什么都没有



我刚刚用ios5安装了xcode 4.2.1,并尝试使用json。

这是我的代码

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

这段代码适用于xcode3,但不适用于xcode 4。

请给我一些帮助。

加载完数据后,需要告诉表视图重新加载。我在你的代码中没有看到。试试这样的东西:

-(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连接。否则,您的reloadData命令将被置若罔闻。

最新更新