0
votes

I have a cell which contains 2 multi-lines labels and an UIImageView. Cell has a dynamic height and it's calculated automatically using Autolayout. My problem is that I download an image in cellForRowAtIndexPath: and until then I don't know the height of the image. At the beginning the cell doesn't has the correct layout, I have to scroll to other cells and when I go back to my cell (it re-draws) then layout is correct. How can I re-draw the cell when the image is downloaded? Here's the code I'm using:

NSString static * cellIdentifier = @"titlePostCell";

PostTitleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) {
    cell = [[PostTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.titleLabel.text = self.entry.title;
cell.descriptionLabel.text = self.entry.entry_description[@"value"];

[cell setNeedsUpdateConstraints];


[cell.postImageView pin_setImageFromURL:[[CTImageHelper sharedInstance] resizedImageURLConverterFromStringWithPrefix:self.entry.card_image] completion:^(PINRemoteImageManagerResult *result) {

    cell.heightSize = [NSNumber numberWithInt:result.image.size.height/2];
    cell.heightImageConstraint.constant = cell.heightSize.floatValue;

}];

return cell;

Thanks!

1
You need to make autolayout of cell such that cell's height should be having dependency of uiimageview height. I don't think giving constraints in cellForRowIndex within block is good way.nikhil84
@nikhil84 cell depends on image size and both labels, as they are multilines and they dont have a fixed sizeJoan Cardona

1 Answers

0
votes

If you have a pointer to the cell (e.g. in your block), you can accomplish this via the following code:

NSIndexPath *indexPath = [tableView indexPathForCell:cell];
[tableView reloadRowsAtIndexPaths:@[indexPath]];

However, I would argue that in general, resizing table view cells without the user specifically requesting you to do so is a poor UI practice. Imagine the UI jumping around on a user as they're trying to use it! Instead, I would use a fixed size for your UIImageViews in your cells. You can place an image of any size into such an image view by setting its contentMode to UIViewContentModeScaleAspectFit.