[英]How can I display an image (dynamically retrieving from a JSON service) in the TableViewCell
I get the url image from a JSON service. 我从JSON服务获得了网址图片。 I know how to display it on a screen using an
UIImageView
object but I don't know how I can do it in a UITableViewCell
cause I cannot drag and drop the UIImageView
object onto a prototype cell. 我知道如何使用
UIImageView
对象在屏幕上显示它,但是我不知道如何在UITableViewCell
因为我无法将UIImageView
对象拖放到原型单元上。 I checked some of the resources but they are all very confusing and the difference between displaying an image on the screen and on the UITableViewCell
is not clear. 我检查了一些资源,但是它们都很混乱,并且在屏幕上和在
UITableViewCell
上显示图像之间的区别尚不清楚。 I am sure replies of this post will help solving this confusion for a lot of developers like me! 我相信这篇文章的回复将有助于解决许多像我这样的开发人员的困惑!
My question is, what is the difference between displaying an image on the screen and on the UITableViewCell
in code level? 我的问题是,在代码级的屏幕上和
UITableViewCell
上显示图像之间有什么区别? (Do we use UIImageView
object in UITableViewCell
as well? Do we drag and drop anything? Do we need contentView
or subview
? etc.) (我们是否还在
UITableViewCell
使用UIImageView
对象?是否拖放了任何东西?是否需要contentView
或subview
等?)
Have a look at this really nice tutorial. 看看这个非常好的教程。 It should not leave any of your questions open.
它不应让您有任何疑问。
http://www.markj.net/iphone-asynchronous-table-image/ http://www.markj.net/iphone-asynchronous-table-image/
I don't suggest to follow this approach.A table view has many rows so the image would be too tiny.Do it only if the image are like banners. 我不建议采用这种方法,因为表视图有很多行,所以图像太小了,仅当图像像横幅时才这样做。
Solution 解
If you still want to upload every image in a different row (so in a cell), you can return a cell with a UIImageWiew inside it: 如果您仍要上传不同行中的每个图像(因此在一个单元格中),则可以返回一个带有UIImageWiew的单元格:
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* path;
... // assign the path according to the index path, so that it loads
... // a different image for every row and section
UIImage* image=[[UIImage alloc]initWithContentsOfFile: path];
UITableViewCell* cell=[UITableViewCell new];
[cell.imageView setImage: image];
return cell;
}
Alternative Solution 替代解决方案
If instead you want a big image to be displayed, just monitor the row selection, put a big image well below the table view and change the image well contents when the row selection changes. 相反,如果您希望显示大图像,则只需监视行选择,将大图像放在表格视图的下方,并在行选择更改时更改图像的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.