我有一个自定义表,我想显示来自json的图像,但它在所有行中显示相同的图像

时间:2014-07-26 05:16:51

标签: ios json uitableview uiimageview tablecell

 // 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]]]];

2 个答案:

答案 0 :(得分:0)

首先在接口部分全局创建一个NSMutableArray并在viewDidLoad方法中初始化

NSMutableArray *arr;

之后在url_Img_FULL

中添加arr
[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 return arr.count。

答案 1 :(得分:0)

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;