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

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.

Related

tableView cell dynamic height only works properly after some scrolling

I'm trying to set the height of my tableview cell based on the height of an image inside my cell, the cell is rendering properly if I scroll a little up and down otherwise it is not getting updated, I have set all necessary constraint the image view (leading, trailing, top, bottom).
and i'm setting the image in my cellforrowat
I have set rowHeight as UITableViewAutomaticDimension
I have tried these solution
1. aspect constraint
2. dynamic cell height scrolling issues solutions
make sure you have set top-bottom constraints in storyboard.
save the thumbnail image as data to local storage (I'm using core data)
using the thumb image, calculate the aspect of the image
customise the row height as you want in heightForRowAt method.
I have answer this here:https://stackoverflow.com/a/61768782/7514688

How to add image in ImageView of TableController properly?

I have a TableViewController and custom cell. Inside of the cell i have an ImageView. Xcode 9.4
*I do not have enough reputation - so provide links.
Click for image
I have constraints settled to superview Leading-Trailing: 20, Top-Bottom:10, clip to bound enabled, Scale to fill.
When i launch application, i oversee this:
Click for image
Image shrinks down. If i change to Aspect Fit/Aspect Fill the image becomes smaller(image view is not filled) or zoomed(not all image is available).
How to fix this issue? I do not want to give it specific width/heigh for compatibility.
Maybe i should change my constraints? I have watched several tutorials, where these constraints work, even did step by step, however it does not work as supposed to.
All constraints are settled for ImageView to superView (that is cell itself).
Thanks.
Set estimated height for your cell self.tableView.estimatedRowHeight = CGFloat(44) and also tableView.rowHeight = UITableViewAutomaticDimension so that it will grow according to your content inside. the problem here is with your image your cell getting height from your image you provided if you give fix height for your image your problem would be solved.
to adapt height for imageView according to iPhone and iPad.
First way is to get superView height and give your image height according to that programmatically.
Second you can get current device type and give your image you height to that UIDevice.current.userInterfaceIdiom == .pad. you can't do it in storyboard because you don't have access to superView in custom cell.

Custom UIView resize according to content

I have a UITableview which needs to displays content(Image and text) based on the size of the image. (full width - height according to aspect ratio)
I have defined a custom cell and embedded Customview inside it.
Customview(It has imageview constrained to fit the Customview).
CustomView receives the image size from api. I calculate the height needed for Customview.
I have tried setting intrinsicContentSize of Customview according to size received of the image
As well as setting height constraint of the image.
But Height of the container does not reflect in UITableViewAutomaticDimension tableview.
However if set the height of the CustomView inside the cell it works.
Whats the best way to accomplish a custom view which signals the superview the height it needs to display?
Eg - http://ioscake.com/proper-usage-of-intrinsiccontentsize-and-sizethatfits-on-uiview-subclass-with-autolayout.html

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.

How to show the text in label from left to right depending on whther or not there is another UI Element present

I am trying to show some content in my Table view cell. I have an image on the left and I have to show some text (Description ) on the right. Everything is dynamic. So the length of the description if sometimes small and sometimes very very large.
Now, I have a Image view on the left and description on the right .Please find the below image for reference
.
I want to show the description occupy the entire tableview cell width if the description slides down even after the image view. How can i achieve this ?
I would suggest you use Auto-layout for this. You'll need to set the height and width constraints for your imageView. Don't specify width for your textview/label, instead specify the leading and trailing constraints for the it. Create an outlet for the width constraint of the imageview. If you don't have an image to display in the imageview add a condition check to set it to 0, otherwise set it to the constant value. If the imageview's width becomes zero, the textview will expand and it will decrease if the you set a constant value for the width constraint. Also, in your tableview delegate return UITableViewAutomaticDimension in both heightForRow and estimatedHeightForRow.

Resources