2013-10-09 45 views
4

我使用AFNetworking將圖像插入到UITableViewCell中。問題是我需要滾動表格才能看到圖像。AFNetworking UIImageView需要滾動以加載圖像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    Recipe *recipe = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = recipe.value; 
    NSURL *url = [NSURL URLWithString:[BaseURLString stringByAppendingString:recipe.img]]; 
    [cell.imageView setImageWithURL:url]; 
    return cell; 
} 
+0

你能證明你的setImageWithURL? – wesley

+0

它是一種AFNetworking方法。我正在考慮用自己的方式做一個成功塊,然後在UITableView上使用reloadRowAtIndexPath方法,但必須是一些技巧,它是一個普遍的問題,想想 – Godfather

回答

4

AFNetworking在UIImageView + AFNetworking類別中已經實現了這樣一種方法。

[imageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
    blockImageView.image = image; 
} failure:nil]; 

注意,如果在零傳遞成功的圖像設置給你打電話的方法ImageView的。在我的情況下imageView。

所以在這裏,我猜你可以重新加載數據,如果你需要。

+1

[cell.imageView setImageWithURLRequest:req placeholderImage:nil success: ^(NSURL請求*請求,NSHTTPURL響應*響應,UIImage *圖像)cell.imageView.image = image; [tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }失敗:無];如果它可以幫助任何人 – Godfather

1

Peter Segerblom's answer之外,我建議使用__weak引用代替__block blockImageVariable變量以避免保留週期。

如果您只是使用__block,imageView的副本將永遠不會被釋放,並且即使在tableView因爲指針正在引用另一個內存地址而消失之後,count也將始終保持不變。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    cell.textLabel.text = @"Cell text"; 

    NSURLRequest *request = [NSURLRequest requestWithURL:imageURL]; 
    __weak UITableView *weakTableView = tableView; 
    __weak UITableViewCell *weakCell = cell; 
    __weak UIImageView *weakImageView = cell.imageView; 
    [cell.imageView setImageWithURLRequest:request 
          placeholderImage:placeholderImage 
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
             weakImageView.image = image; 
             if ([weakTableView.visibleCells containsObject:weakCell]) { 
              [weakTableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone]; 
             } 
           } failure:nil]; 

    return cell; 
} 
+1

這對我很好 – CppChase

1

@pxpgraphics:我不得不更換

NSURLRequest *request = [NSURLRequest requestWithURL:imageURL]; 

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:imageURL]]; 

防止運行時錯誤。

1

斯威夫特相當於:

let request = NSURLRequest(URL: book.mediumImageURL) 
cell.imageView.setImageWithURLRequest(request, placeholderImage: nil, success: { [weak cell] request, response, image in 
    if cell { 
     cell!.imageView.image = image 
    } 

    if tableView.visibleCells().bridgeToObjectiveC().containsObject(cell) { 
     tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None) 
    } 
}, failure:nil) 
0

這個帖子有問題的解答:不必根據索引路徑重新加載電池http://jabbleashish.blogspot.com/2013/12/setting-uitableviewcell-imageview-via.html

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR_URL"]]; 
[cell.imageView setImageWithURLRequest:imageRequest placeholderImage:nil 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
     NSLog(@"success"); 
     cell.imageView.image = image; 
     cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 
     cell.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.} 
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ 
     NSLog(@"Failure");} 

這似乎簡單。

1

我覺得這是用最好的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    NSURL *url = [NSURL URLWithString:@"http://www.myimageurl.com"]; //Change this URL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    __weak UITableViewCell *weakCell = cell; 
    __weak UIImageView *weakImageView = cell.imageView; 
    [cell.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"granaziOrange"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     weakImageView.image = image; 
     [weakCell setNeedsLayout]; 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     NSLog(@"Failed to get image"); 
    }]; 
    return cell; 
} 
0

這取決於細胞類型 - 如果你有自己的UIImageView的自定義單元格內,請確保你不叫其出口「 ImageView的「!因爲每個單元格內都有內部的imageView。 我通過重命名鏈接IBOutlet中的東西,如* myImageView解決了這個問題,現在一切正常,代碼:

[cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://placehold.it/100x100"]];