Gajendra K Chauhan Gajendra K Chauhan - 6 months ago 10
iOS Question

Getting same images in UITableView cell in Dispatch_queue

I am adding images from server. I am using NSMutableArray and custom UITableViewCell. Problem : When I run the project. UITableViewCell displaying same images. I think it refreshing cell. How can I fix that issue?

Below dispatch method I used,

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSError *nserror = nil;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [imagesArray2 objectAtIndex:indexPath.row]]options:NSDataReadingUncached error:&nserror];

//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
if (nserror)
{
[cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
}
else
{
[cell.imgview setImage:[UIImage imageWithData:imageData]];
}
[mytablview reloadData];
});
});


Thanks for any help.

Answer

Just reset cell imageView, before loading new image.

   [cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
   dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(concurrentQueue, ^{
            NSError *nserror = nil;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [imagesArray2 objectAtIndex:indexPath.row]]options:NSDataReadingUncached error:&nserror];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (nserror) {
                    [cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
                } else {
                    [cell.imgview setImage:[UIImage imageWithData:imageData]];
                }
            });
        });