UITableView数据需求

时间:2013-05-29 12:52:35

标签: ios uitableview

您好我正在尝试按需加载数据。 在第一次加载数据后,我调用以下方法。

-(void)carregaDados2
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=12&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;

    NSMutableData *data2;
    data2 = [[NSMutableData alloc] init];
    [data2 appendData:response];
    NSMutableData *dt = [[NSMutableData alloc] init];
    [dt appendData:data];
    [dt appendData:data2];
    news = [NSJSONSerialization JSONObjectWithData:dt options:nil error:nil];  
    [tableViewjson reloadData];
    NSLog(@"Erro: %@", jsonParsingError);
}

但是我的桌面视图是空白的。

我做错了什么?


我真的不知道还能做什么。

现在我改变了我的代码,我不能把我的第二个索引NSMutableArray放在我的UITableView中

-(void)carregaDados2
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=12&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;

    NSMutableData *myData = [[NSMutableData alloc]init];
    [myData appendData:response];


    NSMutableArray *news2;

    news2 = [NSJSONSerialization JSONObjectWithData:myData options:nil error:&jsonParsingError];
    NSLog(@"LOG: %@", news2);
}

1 个答案:

答案 0 :(得分:2)

从它的外观来看,你将两个JSON响应缓冲区粉碎在一起,并尝试将它们解析为单个JSON消息。

[dt appendData:data];
[dt appendData:data2];
news = [NSJSONSerialization JSONObjectWithData:dt options:nil error:nil];  

这不起作用。

例如,如果data[{"x"="a","y"="b"}]data2[{"x"="c","y"="d"}],则dt的值为[{"x"="a","y"="b"}][{"x"="c","y"="d"}],这是无效的JSON消息

我建议将JSON消息解析为分别组合两个数组的NSArrays。


组合两个数组是一个基本的NSArray / NSMutableArray操作。

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *error = nil;

NSArray *updatedNews = [NSJSONSerialization JSONObjectWithData:response options:nil error:&error];

// If news is of type NSArray…
news = [news arrayByAddingObjectsFromArray:updatedNews];

// If news is of type NSMutableArray…
[news addObjectsFromArray:updatedNews];

[tableViewjson reloadData];
相关问题