在iOS上,如何让cell.imageView刷新其内容? [英] On iOS, how to make a cell.imageView refresh its content?

查看:130
本文介绍了在iOS上,如何让cell.imageView刷新其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 tableView:cellForRowAtIndexPath 通过调用来设置图像

I am experimenting in tableView:cellForRowAtIndexPath to set an image by calling

[self downloadImage:urlString andSetIntoCellImageView:cell]

downloadImage ,它将调用 NSURLConnection:sendAsynchronousRequest (仅限iOS 5及更高版本),并在完成块中,使用

and in downloadImage, it will call NSURLConnection:sendAsynchronousRequest (iOS 5 and up only), and in the completion block, set the image by using

cell.imageView.image = [UIImage imageWithData:data];   // data is downloaded data

如果在 tableView:cellForRowAtIndexPath imageView 填充了虚拟占位符图像 - 我想知道新图像是如何刷新的,是 setNeedsDisplay 进行重新绘制?但是,如果我没有设置占位符图像,那么新图像将根本不显示。我想知道可以使用什么机制来显示图像?

and it works if in tableView:cellForRowAtIndexPath, the imageView is populated with a dummy placeholder image -- and I wonder how the new image is refreshed, is it by setNeedsDisplay to do a repaint? But if I don't set the placeholder image, then the new image won't show at all. I wonder what mechanism can be used to make it show the image?

如果我使用

[cell.imageView setNeedsDisplay]

[cell setNeedsDisplay];

在完成块中,它将无效,如果我使用

in the completion block, it won't work, and if I use

[self.table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                            withRowAnimation:UITableViewRowAnimationAutomatic];

indexPath ,它将再次调用 tableView:cellForRowAtIndexPath ,并导致无限循环。所以我似乎需要使用一些哈希表来记住图像是否已经在哈希表中:如果没有,请调用 downloadImage ,如果在哈希表中,只需使用它,所以没有无限循环。

in the completion block by making downloadImage accept the indexPath, it will call tableView:cellForRowAtIndexPath again, and cause an infinite loop. So it seems like I need to use some hash table to remember if the image is already in hash table: if not, call downloadImage, and if in hash table, simply use it, so there will be no infinite loop.

但是有一种简单的方法可以让图像显示出来吗?设置占位符图像有效,但如果我们不这样做 - 通过什么机制占位符导致图像刷新?

But is there an easy way to cause the image to show up? Setting a placeholder image works but what if we don't -- by what mechanism does placeholder cause the refresh of image?

推荐答案

当调用 UITableViewCell -layoutSubviews 方法时,如果其 imageView 图片属性 nil imageView 给出一个(0,0,0,0)的框架。此外, -layoutSubviews 仅在某些情况下被调用:当单元格即将变为可见时以及何时被选中。不是在正常滚动期间。所以你看到的是在 tableView:cellForRowAtIndexPath: sizes cell.imageView 中将占位符设置为非零大小和随后的图像更改将可见。

When a UITableViewCell's -layoutSubviews method is called, if its imageView's image property is nil, imageView is given a frame of (0,0,0,0). Also, -layoutSubviews only is to be called in some situations: when the cell is about to become visible and when it is selected. Not during normal scrolling. So what you've seen is that setting the placeholder inside tableView:cellForRowAtIndexPath: sizes cell.imageView to a non-zero size and subsequent changes of the image will be visible.

我通过调用 [cell setNeedsLayout] 修复了此问题在完成处理程序中,如下所示:

I fixed the issue by calling [cell setNeedsLayout] in the completion handler, like so:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:MY_IMAGE_URL]];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:self.operationQueue
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                                UIImage *image = [UIImage imageWithData:data];
                                                cell.imageView.image = image;
                                                [cell setNeedsLayout];
                                            }];

我发现完成块在后台发生,因此需要在主线程上执行我的UI工作。当然这个解决方案不会考虑细胞再利用等等,但至少解决了细胞图像不会出现的原因:)

I found the completion block happens in the background so that necessitates performing my UI work on the main thread. Of course this solution won't account for cell reuse and so forth, but at least solves why the cell's image wouldn't appear :)

希望这会有所帮助!

这篇关于在iOS上,如何让cell.imageView刷新其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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