Nevin Jethmalani
Nevin Jethmalani

Reputation: 2826

Image getting resized after download in tableview cell

I am getting an image from a URL in a tableview cell. The image view is hugging the right top and bottom of a cell in the tableview cell. To the left of the image is text. I want the text to set the height of the tableview cell automatically and I want the image to conform to the size that is set by the text. How would I do that? Right now it is working but when the image is being downloaded, the cell resizes to become much larger because it uses the large dimensions of the image. As a result the cell in the tableview gets really tall. How would I fix this?

I know the issue is because I am using a greater than or equal to constraint between two of my labels as you can see below in the screenshot. But I need that greater than or equal to constraint.

Here is what my constraints look like:

enter image description here

This is what they look like and what I want it to look like: enter image description hereenter image description here

Upvotes: 0

Views: 72

Answers (2)

matt
matt

Reputation: 535306

This seems to be a cell that is laid out more or less as you desire:

enter image description here

The first label has three lines. The second label has four lines. The third label has one line. The first label has a leading constraint and a trailing constraint to the cell content view; the other two labels have their leading and trailing edges aligned to it. There are four constraints from top to bottom, content view to first label to second label to third label to content view.

The image view has its top aligned to the first label top, its bottom aligned to the third label bottom, its leading edge constrained to the first label trailing edge, and its trailing edge constrained to the content view.

That's all.

EDIT Sorry, I omitted a piece of the puzzle. For your use case, the image view's vertical content compression resistance would need to be lower than any label's vertical hugging priority. That says: "Let the labels dictate how tall I can be." Your labels have a vertical hugging priority of 251, so 250 would do.

enter image description here

Upvotes: 1

Do2
Do2

Reputation: 1791

You have set your constraints for the 3 labels, now for the image view set equal height to your cell(90-95% should do it). So the labels control the height of the cell and then the cell controls the height of the image view.

Edit: I think that you are complicating things. All your cells will have the same height(title,preview, source of 3,4,1 lines respectively), let them set the height, you don't need content hugging priority with the way i am suggesting. I would use equal widths to set the width for all the items in your cell, the i would set the horizontal centers, for the spacing between constraints you can either use top-bottom constraints or set vertical centers and you are done.

Bare in mind that top-bottom-trailing-leading are NOT always the best choice, sometimes (like this one) can cause headache.

Upvotes: 0

Related Questions