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?