1
votes

I'm working on an app which uses tableviews which displays content fetched from a webservice. In some cells I may need to display an image with unknown dimensions. To do this, I have an UIImageView inside the cell of a fixed width (292pts). I then adjust the imageview height accordingly, like this.

imageviewheight = 292/imagewidth*imageheight

For example, if I have an image that is 730*1250 I set the height to 500 to maintain the aspect ratio and show the full image.

However, this can be a problem when I need to set the cell height. Since the images are downloaded from the internet, the image may not have finished downloading when I need to create the cell. Thus, I cannot determine the cell height until the images have downloaded, and I can't have my UI wait for that, so I display a placeholder image instead, and the cell height is based off the height of that.

Once my image has downloaded I need to display it in the image view. That would be fine, if the height matched the height of the placeholder image. Otherwise, I need to adjust the imageview height to compensate, and in turn change the cell height as well. But, the problem is I can't change the height once the cell has been created.

The only solution that I can think of is saving the UIImage, and reloading the cell. Then, since I already have the image, I can setup the cell height accordingly based off the image. However this seems like a very inefficient solution. Is there any way to adjust the cell height after it has been created? I tried changing the cell's frame, but that didn't do anything. Is there a way to change the cell height after creation, or is reloading the only way?

1
I think this will help you click here. Hope it helps.keen
You have to set Cell Height at any cost before CellforrowatindexPath , what i can suggest you , when you get information of your images, put that images in array, and populate your tableview with that array, and in height for row at index calculate that image height against that index in array,.. Hope that helps youumer sufyan
why you not set fix height and width of image ?Nitin Gohel

1 Answers

1
votes

Once the image is loaded, be sure you're returning the correct new height for the row from your UITableViewDelegate in the method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath. You may need to call - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation to force it to re-check the value for the height of that row.