简体   繁体   English

如何在TableViewCell中显示图像(从JSON服务动态检索)

[英]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对象?是否拖放了任何东西?是否需要contentViewsubview等?)

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.

相关问题 如何使用swift3在TableViewCell中的CollectionViewCell中动态显示Server中的数据? - How to display dynamically data from Server in CollectionViewCell in TableViewCell with swift3? 如何在TableViewCell中显示图像和文本 - How to display image and text in a tableviewcell 如何在直接来自Web服务解析的UIscrollView中显示图像? - how can i display image in UIscrollView which comes directly from parsing of web service? 我如何从网址显示图像? - how can i display image from url? 如何填充TableViewCell LeftDetail? - How can I populate a TableViewCell LeftDetail? 如何显示从文档目录到图像视图的图像? - How can i display images from document directory to image view? 如何显示图片网址中的UIImage - how can I display UIImage from image url 目标C-使用tableview将选择的图像从UIImageView传递/检索到第二个VC,以便单元格可以显示该图像 - Objective C - Passing/retrieving selected image from UIImageView to second VC with tableview so cell can display that image 如何在自定义的tableViewCell中获取UIButton的indexPath? - How can I get the indexPath of UIButton in a customized tableViewCell? 我如何在TableViewCell中使用不同的字体制作不同的单词 - How can i make different words in a tableviewcell with different font
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM