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

}
于 2013-11-02T16:14:34.320 回答