解析JSON文件的链接并下载它们

时间:2013-11-02 15:06:59

标签: ios json facebook

我想创建一个App来解析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;
}

我真的不明白为什么。如果这个问题很愚蠢,我很抱歉,但我的编码只是一种爱好而且没有那么棒的经历。

1 个答案:

答案 0 :(得分:0)

实际上您的连接是异步的,因此您只需要以编程方式重新加载表视图以显示加载的图像:

- (void)connectionDidFinishLoading:(NSURLConnection *)连接    {       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];

  }

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

}
相关问题