John Sloan
John Sloan

Reputation: 367

UITableViewCell Image not updating

I can update the detailTextLabel.text and the UITableViewCell shows the changes at runtime, but if I try to update the imageView.image it does not change the visible image. Any idea as to why? I have tried calling a refresh on the UITableViewCell specifically but to no avail.

-(void)getImageForURL:(NSURL*)url row:(UITableViewCell*)cell {

    UIImage*image;

    image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url];

    cell.imageView.image = image; // Does not work..
    cell.detailTextLabel.text = @"test"; // Works

}

Upvotes: 5

Views: 4984

Answers (4)

Aaron Halvorsen
Aaron Halvorsen

Reputation: 2680

I too was having problems with adding the image to my imageView that was constrained in a tableview cell and was not updating with cell.setNeedsLayout(). Calling update methods on the tableview after the image was added did the trick for me:

cell.setNeedsLayout()
if #available(iOS 11.0, *) {
    tableView.performBatchUpdates(nil, completion: nil)
}
else {
    tableView.beginUpdates()
    tableView.endUpdates()
}

Upvotes: 0

arya
arya

Reputation: 956

Try calling [cell setNeedsLayout] after setting the image, if it's the first image you're setting for the cell.

Upvotes: 22

Costique
Costique

Reputation: 23722

Make sure the cell style is UITableViewCellStyleDefault, because other cell types may always return nil imageView, instead of creating one on demand.

Upvotes: 1

ipraba
ipraba

Reputation: 16543

Check whether the image is nil or not. If its nil then the Image is not fetched from the URL correctly.

Upvotes: 0

Related Questions