简体   繁体   中英

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.

This doesn't really answer your question, but it may help you change the direction you're heading in.

When you finish loading the image, the cell may have already started loading a different image because UITableView s reuse their cells. The operation of asynchronously loading the image and setting it once it's done doesn't get cancelled and so you're going to get incorrect images popping up all over the shop, since there's no guarantee of which image loading finishes first.

Please see my comment regarding AFNetworking , which I highly recommend that you use, but if you go down a different path then you should be using some kind of NSOperation subclass for loading the images. That way you can cancel the operations when the cells are reused and you can then safely load a different image for that cell.

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]];
                }
            });
        });

Save the url in the cell that will ultimately show the image. Then when you attempt to show the image after it arrives make sure the url in the cell matches the url of the image you are trying to show. If it doesn't match don't show it; this means the cell has been reused (due to scrolling or refresh) and no longer requires that image. No need to stop the loading of the image, just the display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM