1

I'm trying to load a image into a table view cell, at the first time the image does not show, but if I refresh the table (using refresh control) and reload the data and the image show perfectly.

**I'm loading the image into the main thread and I'm using a custom cell

UIImage *image = //load image, it always return a value, never nil

if(image != nil){
    self.iconCell.contentMode = UIViewContentModeScaleAspectFill;
    self.iconCell.clipsToBounds = YES;
    self.iconCell.image = image;

    [self.iconCell setNeedsDisplay];
}

what could be the root of this issue?

BlaShadow
  • 11,075
  • 7
  • 39
  • 60

2 Answers2

0

As part of your custom table view cell, you should keep track of which row (or index path) your custom cell is currently displaying.

E.G. add two properties to the custom cell like:

@property (strong) NSIndexPath *currentIndexPath;
@property (weak) UITableView *parentTableView; // important, must be weak!

And then you can set it in your view controller's "cellForIndexPath:" method like:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    ...
    myCustomCell.currentIndexPath = indexPath;
    myCustomCell.parentTableView = tableView;

Now, in the same function where you update the image in your cell (and right after), you can reload the single cell by doing:

UIImage *image = //load image, it always return a value, never nil

if(image != nil){
    self.iconCell.contentMode = UIViewContentModeScaleAspectFill;
    self.iconCell.clipsToBounds = YES;
    self.iconCell.image = image;
}

// reloading now should have the image properly set...
NSArray *rowToReload = [NSArray arrayWithObject:self.currentIndexPath];
[self.parentTableView reloadRowsAtIndexPaths:rowToReload withRowAnimation:UITableViewRowAnimationNone];

More information can be seen in this related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

The problem was that I'm using auto layout and the view does not have the installed checked for the current dimensions that I was testing.

Installing and Uninstalling Views for a Size Class

BlaShadow
  • 11,075
  • 7
  • 39
  • 60