如何在给定URL的表格单元格中显示图像?

时间:2013-04-11 16:20:02

标签: iphone objective-c uiimage

此代码:

cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"recipes"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"time"];

显示单元格的标题和副标题,并从JSON文件中获取数据。

此代码:

cell.imageView.image = [UIImage imageNamed:@"test.jpg"];

向表格视图中的所有单元格显示相同的静态图像。如何使这行代码与前两个代码类似,以便从JSON文件中获取图像链接并将图像显示为缩略图?

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

  cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[news objectAtIndex:indexPath.row] objectForKey:@"URLOfYourImage"]]]

答案 2 :(得分:0)

如果您使用this category,则可以使用一行代码执行此操作。

[cell.imageViewPhoto setImageWithURL:[NSURL URLWithString:@"www.so.com/image.png"] placeholderImage:[UIImage imageNamed:@"placeholder.gif"]];

此外,此类别为您提供延迟加载和缓存功能,我认为这对您的需求非常重要。我在我的项目中经常使用它。