0

I get images asynchronously with parse, and i insert them into an array with some other stuff.

I return this array to fill a tableView, and the rows has an UIImageView, but some images that i get asynchronously doest get loaded when i fill the table with the array in cellForRowAtIndexPath, so when i first look the tableView, i get the rows with an empty image in the cell, till i move the scroll in the tableView so cellForRowAtIndexPath is called again and those images i was set in the array are already loaded.

I get the images also if i do a IBAction with [self.tableView reloadData]; i get the images.

The problem is that i return the cell inmediatelly and the image is not loaded. I'm using a custom TableViewCell. I found something about "AsyncImageView" to load the image, but i dont know how.

    - (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;
        }];
4

2 回答 2

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 >

于 2013-05-16T15:12:26.420 回答
0

如果nil拿到dequeueReusableCell的时候设置成图片,然后让block设置图片呢?

只需更改此:

cell.userImage.image = game.avatarp1; 

为了这:

cell.userImage.image = nil;

然后让这段代码工作(我已经理解你的回答,这很好用)

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

希望能帮助到你!

于 2013-05-16T12:05:05.293 回答