从图像 URL 数组向 UITableViewCell 添加图像 [英] Adding images to UITableViewCell from an array of URL of images

查看:43
本文介绍了从图像 URL 数组向 UITableViewCell 添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像 URL 数组,我想在 UITableView 的单独单元格上显示每个 URL 的图像.在我之前的代码中,我使用了:

 UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 400, 180)];view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arrURL[i]]]];

这是在一个 for 循环中,这样 x、y 和 i 的值会针对每个 UIImageView 更改.但是,当我在 cellForRowAtIndexPath 中使用它时,这种方法不起作用.图像未显示,我对 cellForRowAtIndexPath 的实现如下:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *simpleIdentifier = @"simpleIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];如果(单元格 == 零){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault重用Identifier:simpleIdentifier];}cell.textLabel.text = _arrNames[indexPath.row];UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];imv.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[i]]]];[cell.contentView addSubview:imv];返回单元格;}

此外,当我直接添加图像的 URL 时,图像显示在每一行中,但滚动滞后很多.任何帮助将不胜感激.谢谢.

解决方案

使用 sdwebimage 库 sdwebImage

I have an array of URL of images, and i want to display the image of each URL on a separate cell of the UITableView. In my previous code i used:

    UIImageView *view_Image = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, 400, 180)];

    view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arrURL[i]]]];

This was inside a for loop such that the values of x,y and i would change for every UIImageView. However this approach does not work when i use this in cellForRowAtIndexPath. The images are not getting displayed, my implementation for cellForRowAtIndexPath is as:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = @"simpleIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}

cell.textLabel.text = _arrNames[indexPath.row];

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 300, 150)];
imv.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[i]]]];

[cell.contentView addSubview:imv];
return cell;
}

Also when i directly add the URL of the image, the image is displayed in every row but the scrolling lags a lot. Any help will be appreciated. Thanks.

解决方案

use sdwebimage library sdwebImage

这篇关于从图像 URL 数组向 UITableViewCell 添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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