2013-11-02 2 views
0

Facebook의 사진 링크를 파싱하여 iOS 앱에 표시하는 App을 만들려고합니다 (tableView에 표시). 하지만 JSON 파일을 파싱하고 다운로드해야하는 배열에 추가 한 후에는 아무 것도 표시되지 않습니다.JSON 파일의 링크를 파싱하여 다운로드하십시오.

- (void)viewDidLoad 
{ 
self.edgesForExtendedLayout = UIRectEdgeNone; 
[super viewDidLoad]; 


items = [[NSMutableArray alloc] init]; 
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/HuppFotografie/photos/uploaded/?fields=id,name,picture"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

if (connection) { 
    webData = [[NSMutableData alloc]init]; 


} 

} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[webData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[webData appendData:data]; 
} 


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
NSLog(@"fail with error"); 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; 
NSArray *arrayOfData = [allDataDictionary objectForKey:@"data"]; 

for (NSDictionary *dicton in arrayOfData) { 
    NSString *picture = [dicton objectForKey:@"picture"]; 

    [items addObject:picture]; 

} 



} 


- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 250.0; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [items count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *CellIdentifier = @"MyImageCell"; 
ImageCell *cell = (ImageCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.backgroundColor = [UIColor clearColor]; 

if (cell == nil) { 
    NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageCell" owner:self options:nil]; 
    for (id currentObject in topLevelObjects) { 
     if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
      cell = (ImageCell *)currentObject; 
      break; 
     } 
    } 
} 

// Here we use the new provided setImageWithURL: method to load the web image 
[cell.imageView setImageWithURL:[NSURL URLWithString:[items  objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]]; 

cell.imageSource.text = [items objectAtIndex:indexPath.row]; 

return cell; 
} 

난 정말 왜하지 않습니다 여기에 코드입니다. 이 질문이 어리 석다면 미안하지만 취미로 코딩하고 그 위대한 경험이 없다면.

+0

connectionDidFinishLoading에서 allDataDictionary, arrayOfData 및 items 배열의 값을 확인 했습니까? –

+1

'connectionDidFinishLoading'끝에 테이블보기를 다시로드하려고 시도했을 수 있습니다. 편집 : 자리 표시 자 이미지가 보이십니까? 그렇지 않다면 위의 제 제안이 정확해야합니다. –

+0

네 자리 표시자가 표시되지 않습니다 ... 그래서 당신의 제안은 정확할 것입니다. 나는 내일 집에 없기 때문에 오늘 그것을 확인하십시오. 귀하의 빠른 대답을 주셔서 감사합니다 –

답변

0
방금로드 된 이미지를 표시 할 프로그램 테이블보기를 다시로드해야 귀하의 연결, 비동기 사실

:

- (무효) connectionDidFinishLoading : (있는 NSURLConnection *) 연결 { 있는 NSDictionary * allDataDictionary = [NSJSONSerialization을 JSONObjectWithData : webData 옵션 : 0 오류 : 없음]; NSArray * arrayOfData = [allDataDictionary objectForKey : @ "data"];

for (NSDictionary *dicton in arrayOfData) { 
     NSString *picture = [dicton objectForKey:@"picture"]; 

     [items addObject:picture]; 

    } 

    // Just add this line, in order to force reload when all your data is arrived 
    [self.tableView reloadData]; 

} 
+0

아직 작동하지 않습니다 .. 이제 배열에 링크를 추가 할 노력 수동으로 ans 작동 ... 그래서 어쩌면 뭔가 JSON sterialization 잘못된 것 같아요 유일한 이유는 왜 자리 표시 자도 있습니다. 배열은 비어 있습니다. –

관련 문제