Auto height of UITableViewCell depending on the amount of text UILabel

I have a slightly complicated setup in my storyboard, I have a UIViewController that contains a UITableViewController. In the UITableViewController, I have several prototype cells that I linked to subclasses of uitableviewcell objects in the code.

Using constraints and a storyboard, I would like to change the height of my prototype cell depending on the size that ends with UILabel depending on the text falling into it.

I currently have

UIViewController -- UITableViewController -- -- UITableViewCell ((melchat) prototype cell) -- -- -- ContentView -- -- -- -- UIView ((background) view with drop shadow card type effect) -- -- -- -- -- UIImageView (Avatar) -- -- -- -- -- IUlabel (dynamic (depending on code/input) multi line UILabel) 

How I would like UILabel to resize the UIView (background), and then, in turn, resize this UITableViewCell.

I am using Xcode 8.2.1

I took a screenshot with the layout in the storyboard and applied the restrictions.

enter image description here

Update

I updated my restrictions so that almost everything returns to ContentView and updated the number of uilabel lines to 0, and then also implemented the UITableViewAutomaticDimension code, but it still doesn't work. Please see the code and screenshots below.

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } 

enter image description here

enter image description here

+12
source share
5 answers

Go a little further to Tao's answer.

He's right, you need to use UITableViewAutomaticDimension

In addition, you need to make sure that you set your limits so that all content in the cell is limited to contentView cells. So your shortcut is likely to require restrictions such as

  • Leading limitation for ImageView
  • Upper limit for contentView
  • Lower limit for contentView
  • Trailing restriction for contentView

Make sure the UILabel is set to multi-line (or string = 0) and it should work.

If you use the delegate functions heightForRowAt, make sure you return a UITableViewAutomaticDimension

+24
source

Use UITableViewAutomaticDimension

 tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 300 
+13
source

step1: Give below the restriction of the UIView that is inside the contentView.

  • Leading restriction for contentView
  • Upper limit for contentView
  • Lower limit for contentView
  • Trailing restriction for contentView

step2: Give below restriction on UIlabel

  • Leading restriction for UIImageView
  • Upper limit for UIView
  • Lower limit for UIView
  • Trailing Limit for UIView

step3: Then select the Trailing constraint for IUlabel and select the edit option, then select a constant and select the moreThanEqual option.

step4: set label numberofline = 0

step5: add this code to viewDidLoad()

 yourtableview.estimatedRowHeight = 80.0 yourtableview.rowHeight = UITableViewAutomaticDimension 
+9
source

Swift 4.2 and 5 . As mentioned above, you can set the property of a UITableView object or

 tblView.rowHeight = UITableView.automaticDimension tblView.estimatedRowHeight = 300 

you can also define the same by implementing the UITableViewDelegate methods

 extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 300 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } } 
+5
source

After filling in the UILabel and applying the restrictions, get the height of the label and set it as the height of the UIView.

After this call -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and pass the height to UILabel / UIView to get the actual height for this row

-1
source

Source: https://habr.com/ru/post/1265282/


All Articles