0

Facebookページの画像リンクを解析してiOSアプリに表示するアプリを作成したい(tableViewに表示する)。しかし、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;
}

理由が本当にわかりません。この質問がばかげている場合は申し訳ありませんが、私は趣味としてコーディングしていて、それほど素晴らしい経験はありません。

4

1 に答える 1

0

実際、接続は非同期であるため、ロードされた画像を表示するには、プログラムでテーブル ビューをリロードするだけです。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData オプション:0 エラー:nil]; 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];   

}
于 2013-11-02T16:14:34.320 に答える