UITableViewCell中的UIImageView不会更新 [英] UIImageView inside UITableViewCell does not get updated

查看:272
本文介绍了UITableViewCell中的UIImageView不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义 UITableViewCell 以包括 UIImageView 和一些相关文本。我从互联网得到的图像,而图像加载,我显示一个临时 UIImage



使用单独的线程来获取 UIImage 。一旦下载了 UIImage ,我将在主线程上触发一个方法来设置 UIImageView UIImageView 使用标签的实例



但是无论我做什么,图像都不会改变。细胞仍然显示较旧的图像(对应于不同的行 - 由于细胞的重复使用),并且不反映新的图像。



我甚至试图在中将图片更改为静态图片willDisplayCell:forRowAtIndexPath:

b我尝试在更改 UIImage UITableView 上调用 reloadData >



这里是相关的代码



cellForRowAtIndexPath

  image = [imagesArray valueForKey:[NSString stringWithFormat :@%i,indexPath.row]]; 
if(image == nil){
//显示临时图片。当滚动停止时,分离新线程以获取可见行的图像
NSString * pth = [[NSBundle mainBundle] pathForResource:@folderofType:@png];
image = [UIImage imageWithContentsOfFile:pth];
[imgView setImage:image];
}
else {
[imgView setImage:image];
}

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

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



在线程中,我添加了 UIImage 对象 imagesArray 。所以当下次在 reloadData 上调用 cellForRowAtIndex 方法时,新的 UIImage 应该显示。

解决方案

UITableView的单元格被缓存,所以修改单元格后,没有效果。尝试在更新UIImageView之后发送表 reloadData 消息。


I am creating a custom UITableViewCell to include an UIImageView and some related text. I am getting the images from the Internet and while the images load, I display a temporary UIImage.

I am using a separate thread to get the UIImage. Once an UIImage is downloaded, I am firing a method on the main thread to set the image in the UIImageView (getting the instance of UIImageView using tags)

But whatever I do, the images are not changed. The cells still display older images (corresponding to different row - due to reusing of cells) and new images are not reflected. Log statements show that the method to set the image is being called.

I even tried changing the image to a static image in willDisplayCell:forRowAtIndexPath: but even then the image does not change.

Edit
I tried calling reloadData on the UITableView after changing the UIImage, but the UIImage still does not change.

Here's the relevant piece of code

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

Method which is called on the main thread after an image is downloaded

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

In the thread, I am adding the UIImage object to imagesArray. So when the next time the cellForRowAtIndex method is called on reloadData, the new UIImage should be displayed.

解决方案

UITableView's cells are cached, so modifying the cell after it has been rendered will have no effect. Try sending the table a reloadData message, after updating the UIImageView.

这篇关于UITableViewCell中的UIImageView不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆