leedream
leedream

Reputation: 559

UITableView Data Demand

Hello I am trying to load data on demand. after data has been loaded the first time, I call the following method.

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

However my tableview is blank.

What am I doing wrong?


I really do not know what else to do.

Now I changed my code, I can not put my second index the NSMutableArray in my 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);
}

Upvotes: 0

Views: 161

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

From the looks of it you are smashing together two JSON response buffers and trying to parse them as a single JSON message.

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

This will not work.

For example if data is [{"x"="a","y"="b"}] and data2 is [{"x"="c","y"="d"}] then dt would have the value [{"x"="a","y"="b"}][{"x"="c","y"="d"}] which is an invalid JSON message.

I would suggest parsing the JSON messages into NSArrays separately combining the two arrays.


Combining two arrays is a basic NSArray/NSMutableArray operation.

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];

Upvotes: 2

Related Questions