2013-05-16 90 views
0

我使用parse异步获取图像,并将其插入与其他某些东西的数组中。我回来这个数组来填充tableView,并且行有一个UIImageView,但一些图像,我异步doest得到加载时,我填充表中的数组cellForRowAtIndexPath,所以当我第一次看tableView,我获取单元格中的空图像的行,直到我移动tableView中的滚动,所以cellForRowAtIndexPath再次被调用,并且我在数组中设置的图像已经加载。当图像异步加载时更新tableView

我得到的图像也如果我做一个IBAction与[self.tableView reloadData];我得到的图像。

问题是,我立即返回单元格,图像未加载。 我正在使用自定义TableViewCell。 我发现了一些关于"AsyncImageView"来加载图像,但我不知道如何。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    //Get the game for the row 
    GameModel *game = [self.currentMatches objectAtIndex:indexPath.row]; //HERE i load the images 

    MatchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"GameCell" forIndexPath:indexPath]; 

    cell.userImage.image = game.avatarp1; //I set the image to the cell but isnt loaded yet 

    //If i do this WORKS but i get 2 times the same image (on the game and now) (its a function that i created, that seems is calling tableView or so when assigns the image to the cell, i dont understand why it works 
     [ParseModel getAvatarWithUser:game.p1 completionBlock:^(UIImage *avatar) { 
      cell.userImage.image = avatar; 
     }]; 
+0

只是这样做[yourtableView reloadData]; –

回答

0

如果你设置什么nil图像当你得到dequeueReusableCell,然后让该块置的形象呢?

只要改变这一点:

cell.userImage.image = game.avatarp1; 

对于这一点:

cell.userImage.image = nil; 

然后让该代码工作

[ParseModel getAvatarWithUser:game.p1 completionBlock:^(UIImage *avatar) { 
     cell.userImage.image = avatar; 
}]; 

(我已经通过你的答案,这个工程很好理解)希望能帮助到你!

+0

问题是我设置图像一个数组(所有的图像),然后我必须这样做[ParseModel getAvatar ..,并加载重复相同的图像(2次)。它不是n有必要将nil分配给图像。 – Godfather

0

您可以使用“SDWebImage”作为您在问题中提到的“AsyncImageView”的替代。这很容易实现。我在这里放了一个我自己的代码的例子,在这里我按照您尝试的方式从单元格中的URL下载图像。 (更改我的“视图”为您cell.userImage)

这种方法把一个临时的图像(占位符,并且当图像完全下载,它改变程序自动(没有任何重装或滚动)。

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    if (view == nil) 
    { 
     view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 238.0f, 289.0f)] autorelease]; 
     //here is where you put a placeholder and download in the background the image with an URL. Items is an array of URLs 
     [(UIImageView *)view setImageWithURL:[items objectAtIndex:index] 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
     view.contentMode = UIViewContentModeScaleAspectFit; 
    } 
    return view; 
} 

当你添加SDWebImage到你的项目时,不要忘记添加到你的.m中:#import < SDWebImage/UIImageView + WebCache.h>