我有一个自定义表,我想显示来自 json 的图像,但它在所有行中显示相同的图像
Posted
技术标签:
【中文标题】我有一个自定义表,我想显示来自 json 的图像,但它在所有行中显示相同的图像【英文标题】:i have a custom table and i want to display images from json but it displays same image in all rows 【发布时间】:2014-07-26 05:16:50 【问题描述】: // delegate method that returns json
-(void)repaint:(NSMutableArray *)retrievedData
if (retrievedData.count > 0)
userObj = [retrievedData objectAtIndex:0];
for (userObj in retrievedData)
NSLog(@"%@is the value",retrievedData);
url_Img1=@"http://kiascenehai.pk/assets/uploads/event-images/50x50-thumb/";
url_Img2=userObj.event_dpURL;
url_Img_FULL = [url_Img1 stringByAppendingPathComponent:url_Img2];
NSLog(@"Show url_Img_FULL: %@",url_Img_FULL);
[tableData addObjectsFromArray:retrievedData];
[table reloadData];
//to display data in table:::
[[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]];
【问题讨论】:
把它放到你的for循环中 [[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]]; 其实你是直接调用url_Img_FULL,它只显示最后一张图片 将 url_Img_FULL 添加到一个 nsmutablearray 中 欢迎来到 ***。请添加有关您面临的问题的更多详细信息。复制粘贴您的代码将无济于事。userObj
在两个作用域中使用,无法确定你使用的是哪个userObj
。
【参考方案1】:
首先在接口部分全局创建一个NSMutableArray
,并在viewDidLoad
方法中初始化
NSMutableArray *arr;
然后在arr
中添加url_Img_FULL
[arr addObject:url_Img_FULL];
在cellForRowAtIndexPath
中更改这一行
[[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]]];
到
[[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[indexPath.row]]]]];
同样在numberOfRowsAtIndexPath
返回arr.count。
【讨论】:
yogesh 它无法正常工作。如果 json 得到长响应,应用程序就会崩溃。你有其他想法吗 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[用户长度]:无法识别的选择器发送到实例 0xa4ed500” @user3807662 它与tableview无关,它与其他东西有关,您不能将length
用于对象User
。
由于未捕获的异常“NSRangeException”而终止应用程序
[[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[indexPath.row]]]]];在这里它显示:Thread 1 sigbrit【参考方案2】:
Use GCD to download some thing like this
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void)
//write the code to download image
dispatch_async(dispatch_get_main_queue(), ^
// Capture the indexPath variable, not the cell variable, and use that
[tableview reloadData];
);
);
and cellforrowatindexpath write
if(are.count>0)
[[cell imageView] setImage:[UIImage imageWithData:arr[indexPath.row]]]]];
return cell;
【讨论】:
以上是关于我有一个自定义表,我想显示来自 json 的图像,但它在所有行中显示相同的图像的主要内容,如果未能解决你的问题,请参考以下文章