将 UIImageView 添加到 Tableview
我有一个异步图像加载器,它在 UIImageview 中加载图像(JImage)。我想将它们显示在表格单元格中。显然我无法设置 cell.imageView.image 因为我没有图像,我只有一个视图。
如何将 UIImageview 设置为表格单元格? cell.backgroundView 虽然可以工作,但是会覆盖整个单元格。
JImage代码是:
NSURL *theUrl=[NSURL URLWithString:imageURL];
JImage *photoImage=[[JImage alloc] init];
[photoImage initWithImageAtURL:theUrl];
[photoImage setContentMode:UIViewContentModeRedraw];
[photoImage setFrame:CGRectMake(0.0f, 0.0f, 40.0f, 65.0f)];
cell.backgroundView = photoImage;
[photoImage release];
在JImage.m中:
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
[self setImage:[UIImage imageWithData: data]];
}
I have an asychronous image loader which loads images (JImage) in a UIImageview. I want to display these in a tablecell. Obviously i cant set cell.imageView.image because i dont have an image, i just have a view.
How do i set the UIImageview to the tablecell? cell.backgroundView works though, but that paints over the whole cell.
The JImage code is:
NSURL *theUrl=[NSURL URLWithString:imageURL];
JImage *photoImage=[[JImage alloc] init];
[photoImage initWithImageAtURL:theUrl];
[photoImage setContentMode:UIViewContentModeRedraw];
[photoImage setFrame:CGRectMake(0.0f, 0.0f, 40.0f, 65.0f)];
cell.backgroundView = photoImage;
[photoImage release];
which in the JImage.m:
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
[self setImage:[UIImage imageWithData: data]];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此代码并根据您的需要设置框架。
use this code and set frame according to your need.
如果您只想异步加载图像,则不需要使用 JImage。一种简单的方法是使用 GCD 和块,如下所示: https://github.com/ChrisTec/iPhone-Book-CodeSamples/blob/master/Chapter%2013/RealEstateViewer%2013.2.5/RealEstateViewer/ImageTableViewController.m
这是示例章节我与人合着的《Objective-C Fundamentals》一书深入解释了这段代码:http://www.manning.com/fairbairn/OCF_sample_ch13.pdf
总结一下:请求单元格,您查看是否已经下载了该图像。如果有,则显示它。如果没有,您将显示一个微调器并启动一个异步 GCD 块来获取图像。该块完成后,它将在主线程上运行另一个块,以切换图像的旋转器。就这样。
You don't need to use JImage if all you want to do is load the image asynchronously. One easy way to do this is with GCD and blocks, like so: https://github.com/ChrisTec/iPhone-Book-CodeSamples/blob/master/Chapter%2013/RealEstateViewer%2013.2.5/RealEstateViewer/ImageTableViewController.m
Here's a sample chapter of the book Objective-C Fundamentals which I've co-authored that explains this code in depth: http://www.manning.com/fairbairn/OCF_sample_ch13.pdf
To sum it up: When the cell is requested, you look if you already have downloaded the image. If you have, you display it. If not, you display a spinner and start an async GCD block that will fetch the image. One that block is done, it will run another block on the main thread that switches the spinner out for the image. That's all.
我认为你应该采取不同的方法来解决这个问题。不必将 JImage 放入单元格中,而是将其保存在视图控制器中的其他位置,
NSArray
通常很方便。然后用占位符图像填充单元格或将其留空。因此,假设您有一个包含正在加载的所有 JImage 的数组,当数组位置 x 中的 JImage 完成加载时,您将重新加载该特定单元格,如下所示:
在您的
cellForRow:AtIndexPath
您只需从数组中保存的 JImage 对象中获取图像即可。我希望这对你有帮助
I think you should take a different approach to this. Instead of putting your JImage in the cell, hold it somewhere else in the view controller, an
NSArray
is usually convenient. Then populate the cells with either placeholder images or just leave them empty.So let's say you have an array with all the JImages that are loading, when the JImage in the position x of the array finishes loading, you reload that specific cell as it follows:
And in your
cellForRow:AtIndexPath
you just have to take the image from the JImage object held in the array.I hope this helps you