2013-05-29 2 views
0

안녕하세요. 요청시 데이터를로드하려고합니다. 데이터를 처음로드 한 후 다음 메소드를 호출합니다.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); 
} 

그러나 내 tableview는 비어 있습니다.

내가 뭘 잘못하고 있니?


정말 그밖에 무엇을 해야할지 잘 모릅니다.

지금 내 코드를 변경, 난 당신이 두 개의 JSON 응답 버퍼를 스매싱 단일로 구문 분석하려고하는 그 모습에서 내 jQuery과

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

당신은 데이터 소스를 게시하고 tableView의 메소드를 위임 할 수 있습니다. –

+1

테이블 뷰의 dataSource 속성에 아무 것도 지정하지 않았습니까? 또 다른 한가지는 동기 요청을 보내는 것이 좋은 생각이 아니라는 것입니다. –

+0

당신은/setDelegate/print 데이터 등을 걸어 본 적이 있습니까 ... –

답변

2

내 두 번째 인덱스있는 NSMutableArray를 넣을 수 없습니다 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 잘못된 JSON 메시지입니다 값 [{"x"="a","y"="b"}][{"x"="c","y"="d"}]있을 것입니다.

두 배열을 별도로 결합하여 NSArrays로 JSON 메시지를 구문 분석하는 것이 좋습니다.


두 개의 배열을 결합하는 것은 기본 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]; 
+0

정말 그밖에 무엇을 해야할지 모르겠군요. 이제 코드를 변경했는데 두 번째 인덱스 인 NSMutableArray를 UITableView에 넣을 수 없습니다. – leedream

+0

@leedream 배열을 결합하는 방법을 보여주기 위해 제 대답을 업데이트했습니다. –