【问题标题】:json ios5 nothing at the screenjson ios5 屏幕上什么都没有
【发布时间】:2012-01-08 22:03:18
【问题描述】:

我刚刚用 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); 中,我可以看到所有记录,一切正常。但在我的 tableview 中,屏幕上什么也没有。

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

请大家帮忙。

【问题讨论】:

    标签: json uitableview ios5 xcode4.2


    【解决方案1】:

    加载完数据后,你需要告诉你的 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 在设计器中连接。否则你的reloadData 命令会被置若罔闻。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2020-12-11
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多