Pankil
Pankil

Reputation: 645

Change `UIView` Width as per `UILabel` Text in `UITableViewCell`

In my UITableViewCell there's UIView with Width Constraint and inside it there's Image and UILabel.

Upvotes: 0

Views: 212

Answers (3)

KKRocks
KKRocks

Reputation: 8322

Add your view inside UIStackView and inside that view add your label and imageView and set proper constraint to label and imageView. and remove all code for calculation of size.

Upvotes: 1

elk_cloner
elk_cloner

Reputation: 2149

Use UIStackView. You don't have to think about anything.

enter image description here

enter image description here

Upvotes: 0

Jon Rose
Jon Rose

Reputation: 8563

Remove the arrAlreadyChangeCellFrames. A cell that was set once for one object can be reset for a different object so it has to be resized.

Tableviews and Collectionview work by reusing the cells. So that even if you scroll very fast there will only be a handful of cells in memory. Because of this there is a very important rule to always follow in cellForRowAtIndexPath: every if must have an else. Because the cell can be reused anything that you set in one case must be unset in the other case.

Also, for most layouts you should not need to set the width of label when the text changes. Just set the width to the maximum size that it is allowed to be and the text will fill up as much as it can.

Upvotes: 0

Related Questions