UITableViewCell中的UIImageView没有得到更新

时间:2009-02-13 23:01:49

标签: iphone cocoa-touch uitableview

我正在创建自定义UITableViewCell以包含UIImageView和一些相关文字。我从互联网上获取图像,当图像加载时,我显示一个临时的UIImage

我使用单独的线程来获取UIImage。下载UIImage后,我在主线程上触发一个方法,在UIImageView中设置图像(使用标记获取UIImageView的实例)

但无论我做什么,图像都不会改变。单元格仍然显示较旧的图像(对应于不同的行 - 由于重复使用单元格)并且不反映新图像。日志语句显示正在调用设置图像的方法。

我甚至尝试将图片更改为willDisplayCell:forRowAtIndexPath:中的静态图片  但即使这样,图像也不会改变。

修改
我在更改reloadData后尝试在UITableView上调用UIImage,但UIImage仍然没有更改。

这是相关的代码

的cellForRowAtIndexPath

        image = [imagesArray valueForKey:[NSString stringWithFormat:@"%i",indexPath.row]];
        if(image == nil){
            //display temporary images. When scrolling stops, detach new thread to get images for rows visible
            NSString *pth = [[NSBundle mainBundle] pathForResource:@"folder" ofType:@"png"];
            image = [UIImage imageWithContentsOfFile:pth];
            [imgView setImage:image];
        }
        else{
            [imgView setImage:image];
        }

下载图像后在主线程上调用的方法

-(void)setImageInArray:(NSIndexPath *)indPath{
    [filesTable reloadData];
}

在线程中,我将UIImage对象添加到imagesArray。因此,下次在cellForRowAtIndex上调用reloadData方法时,应显示新的UIImage

1 个答案:

答案 0 :(得分:5)

UITableView的单元格被缓存,因此在渲染单元格之后修改单元格将不起作用。在更新UIImageView后,尝试向表发送reloadData消息。