Dynamically sizing UITableViewCells - swift

I have worked with dynamically sizing UITableViewCell before. But currently I am facing a problem statement where I am unable to achieve dynamic sizing using autolayout
My cell has three components viz-a-viz one UIImageView, two UILabel
The structure of the cell is as follows
UIImageView has to be of fixed width and height in my case 60X60
UIImageView will be vertically centered inside the UITableViewCell
UIImageView is having a leading constraint of 15 from the UITableViewCell
Now the two UILabels are vertically placed
Both the labels can have dynamic height and I want them to push expand the cell using autolayout but I am only able to achieve keeping the top UILabel of constant height and the below UILabel increases in height.
I want both the UILabel to expand vertically and increase the size of UITableViewCell.

Just use StackView for both label and make both labels numberOfLine to 0 and line break should be WordWrap. give leading,trailing,top and bottom constraints to stack view. for spacing between labels give spacing for stackview on xcode detail pannel.

See the Screenshots for the setting of the constrains.
Add a height constrain to the label with the constant greaterthanEqualTo a fixed value (see screenshot 2). and set the lines to 0. In the table view heightForRowAt indexPath set that to UITableViewAutomaticDimension .

Related

Table view cell with label and textField and equal space between each other

i want to create a table view with cells which have a textField.
What drives me crazy is the spacing between label and textField cause i want a equal spacing in all cells.
To better explain take a look at this screenshot.
So im searching for a good way to get the spacing between Second Label and Second textField and set this on the other cells, too.
Is there a way to do it in the storyboard and not by edit the constraint in code?
I would suggest that:
use a horizontal stackview to hold the label and textField with some spacing in between, like 10.
set the label's width constraint to a fixed value.
the details are as follows:
add a horizontal stackview into the cell's contentView
set the stackview constraints to fill the contentView with some margins:
eg. leading: 20, trailing 20, top 10, bottom 10.
add a label and a textField into the stackview.
in the stackview's Attribute inspector, set the spacing to 10.
set the label's width constraint to 100, or 0.3:1 to the contentView's width
so the label will have a fixed width, fixed spacing between label and textField, and the textField will take all the horizontal space remain.

Random table view cell height is not working perfectly

I am designing a chat app in which I am using the UITableViewAutomaticDimension property of table view when I am setting the label lines to 6 then all cells height is according to the text which label contains but when I increase the number of lines for label then some cells have extra height. Even when scrolling it then cell height again changed.You can check the image so that you can understand it easily thanks.
You need to give label constraints from top and bottom, rather than center vertically.
Give vertical content hugging priority

Swift tableView rows dynamic height not working

Trying to get my messages in the tableView to have automatic height in each row but currently the long label texts are cut. Any help appreciated!
This function is called from viewDidLoad() and it is called for each row(tested with print):
All constraints are set in the storyboard:
Lines are set to 0:
Constraints for label:
Resulting in no dynamic height but cutting the message instead:
I think your issue might be because your label is set in a UIView. What is your UIView constraint? Your UIView shouldn't have a height anchor for sure and maybe you could try setting it to the same height as your label. (Best guess)

Adjusting the width of a UILabel in UITableViewCell based on the width of a second UIView in the Cell

I have a UITableViewCell with two subviews, a UILabel on the left, and a random input control on the right. The random input control on the right can vary in size, as can the length of the text, but since I can set the word wrap of the text on the left, I need to be able to adjust the size of the UILabel based on the width of the random input control. To complicate matters, the app needs to work in both portrait and landscape modes, which give the table cells different widths.
This wouldn't be difficult if I could read the width of the table cells and set the widths of its subviews appropriately, but at creation time the width of the cell is 0.
Any ideas?
Nothing easier than that: every UITableViewCell is also a UIView, which has a method designed for just that: layoutSubviews, which is called whenever the view (here: cell) needs a re-layout. This is where you lay out the content.

How do I decrease cell size to 280*35?

I need to decrease the table view cell of navigation controller to 280*35. I have tried to with cgrect, but it's not working...
How can I set it?
The width of a UITableViewCell is always determined by the width of the UITableView that contains it.
You can change the height of your Cell by using UITableViews delegate message tableView:heightForRowAtIndexPath:
You can set row height programmatically, as tonclon pointed out, or, if all rows need to get the same height, set it in Interface Builder as a propery of the UITableView, in de Inspector window under the size tab. It says "height" right at the top, that's the row height.
This is easier and (presumably) faster.