1

I am creating a custom UITableViewCell to include an UIImageView and some related text. I am getting the images from the Internet and while the images load, I display a temporary UIImage.

I am using a separate thread to get the UIImage. Once an UIImage is downloaded, I am firing a method on the main thread to set the image in the UIImageView (getting the instance of UIImageView using tags)

But whatever I do, the images are not changed. The cells still display older images (corresponding to different row - due to reusing of cells) and new images are not reflected. Log statements show that the method to set the image is being called.

I even tried changing the image to a static image in willDisplayCell:forRowAtIndexPath: but even then the image does not change.

Edit
I tried calling reloadData on the UITableView after changing the UIImage, but the UIImage still does not change.

Here's the relevant piece of code

cellForRowAtIndexPath

        image = [imagesArray valueForKey:[NSString stringWithFormat:@"%i",indexPath.row]];
        if(image == nil){
            //display temporary images. When scrolling stops, detach new thread to get images for rows visible
            NSString *pth = [[NSBundle mainBundle] pathForResource:@"folder" ofType:@"png"];
            image = [UIImage imageWithContentsOfFile:pth];
            [imgView setImage:image];
        }
        else{
            [imgView setImage:image];
        }

Method which is called on the main thread after an image is downloaded

-(void)setImageInArray:(NSIndexPath *)indPath{
    [filesTable reloadData];
}

In the thread, I am adding the UIImage object to imagesArray. So when the next time the cellForRowAtIndex method is called on reloadData, the new UIImage should be displayed.

Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
lostInTransit
  • 70,519
  • 61
  • 198
  • 274

1 Answers1

5

UITableView's cells are cached, so modifying the cell after it has been rendered will have no effect. Try sending the table a reloadData message, after updating the UIImageView.

codelogic
  • 71,764
  • 9
  • 59
  • 54