Change UITableViewCell Height after creation - ios

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?

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.

Related

Adjust Table View Cell custom view constraint after image loaded from url

In my application, I have a uitableview whose cell height is measured by autolayout UITableViewAutomaticDimension.
The cell contains an image, whose height is adjusted by a NSLayoutconstraint outlet connected to cell.
Image is loaded from a URL. Sometimes the URL will returns "No Image". At this point, I need to change the height constraint to zero.
If an image is available I need to adjust the constraint with a height of 200. The image is loaded from background thread.
But, when I try to update the constraint after image is loaded from web, it doesn't work.
Please suggest a method to resolve the situation.

ImageView in self-sizing table cell uses height of image before Aspect Fit is applied

I have an ImageView inside of a self-sizing table cell that has a content mode of Aspect Fit.
When the table fully loads the image itself looks great, it resizes to fit within the width of the cell while still keeping the original ratio.
However, the height of the table cell is as large as if the Aspect Fit content mode was never applied to the image view.
The red in this image is the image view and the space above and below the actual image is what I'm trying to avoid.
When I replace Aspect Fit with Aspect Fill, this is what the same cell looks like:
This is what I mean when I say it looks like the image view is using the height of the image before Aspect Fit is applied.
Am I missing a constraint or setting in the ImageView?
Any help is appreciated!
If the images are being downloaded from a server, they will take a second or a millisecond to be downloaded. The time they take to download is noticeable to the user. The best user experience is explicitly setting the height and width before the image is downloaded. If you are using a third party API to fetch the image (i.e. facebook or youtube API), the return will include the image URL along with the height and width for the image. This way you can set the height and width of the cell and then wait for the image to download without any interruption or change in the size of cells. This would be the best way to do it. By having the height and width set explicitly, you don't need to worry as much about the content mode to use for the image view. I would only recommend using a self sizing UITableViewCell if you are explicitly setting the height and width of image view within that cell. I hope this helps!
I just saw your most recent comment. You can adjust the size of each image view within each cell depending on the image that is being returned. You don't need to set a fixed height for the image view. You can change the height of each cell depending on the image that is in that cell. I would just recommend doing this before the image is downloaded.
If you are using storyboard, then one way to do this is creating an outlet for the height constraint of the image view.

UITableViewCell Not Sizing To UIImageView

I was wondering if anyone knows hot to fix this issue. When I run the app, the image gets cut off instead of displayed. I have attached photos to show in detail.
Since you have two kind of Cells in the Table View, you have to set the height of both cells programatically inside heightForRowAtIndexPath.
Currently, you have only one cell size and I think it is default to 44.0.
May be you doesn't set imageview's constraints properly.
Set top, bottom, left, rignt constraint of imageview with tableviewcell frame properly. Give a aspect ratio size of the imageview.
And return UITableViewAutomaticDimension from heightForRowAtIndexPath and estimatedHeightForRowAtIndexPath delegate functions.
This will work !!

iOS: UITableViewCell - Cells interrupting/overlaying eachother

I've build a UITableView with different costum cells. One is with a UIImageView and one without. But they are overlaying each other. I've set all constraints.
every cell owns its own section.
screens:
(image and text are samples.)
Does the actual cell height change?
If so, you need to make sure you're returning the correct height with:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return heightOfCell;
}
It looks like you might have the constraints wrong on the cell without the image as it is drawing beyond where the table thinks the height suggests it should end. Hence why it is drawing beyond where the second cell top starts.
Possible issues are:
0) You are returning a height from heightForRowAtIndexPath which is too small. Use auto layout for iOS8 or if suppoting iOS7, you will need to calculate the height.
1) Perhaps your constraints are wrong. Have you pinned the bottom text to the bottom of the cell contentView and the top of the date to the top of the contentView?
2) If using iOS8 auto layout, try adding [self.tableView reloadData]; in viewWillAppear. Sometimes it needs an extra kick to get the layout right, especially if text needs resized. I have no explanation for this other than I had to do this and I've seen it mentioned a few times elsewhere.

Fixing imageview frame according to image

I am showing images downloaded from an URL in a table view whose custom cells have UIImageViews.
Now my requirement: I want to fix the height of that imageview and cell according to the height of that image in that cell.
The steps to perform are the following: (I won't write the code for you, just guide)
in heightForRow method you pick the image from the data source
then you learn its size (image.size)
return the value like
size.height+SOME_CONSTANT, where SOME_CONSTANT is the space for
other content

Resources