4

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)

So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.

In my tableViewController.swift file, I have written this code:

override func viewWillAppear(_ animated: Bool) { tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension }

However, this is not working as when I add more text to the text view, it just cuts off.

Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.

I honestly don't know what I am doing wrong

A picture is below of what my app looks like and if anyone could help that would be greatly appreciated

enter image description here

4
  • Did you set the Content Mode to Scale To Fill in the Interface Builder for your UITextView Commented Jun 26, 2017 at 19:22
  • I found this auto resize hard to use if I have more than one textView inside and I always do height calculation by myself. Commented Jun 26, 2017 at 20:11
  • I'm interested in knowing how you created those spaces between the cells and the rounded shape. Commented Jan 23, 2018 at 11:07
  • I created a UIView in each cell. Then put the contents of each cell in that UIView. The view has 8 pixels of space on each side. And to make the corners round, i used myView.cornerRadius = int Commented Mar 2, 2018 at 20:50

4 Answers 4

11

Check if your constraints are like this(based on your image) :

imageView : set to Top and Leading of your container, with fix height and width values.

label : you can set it to top and horizontal space of your image, with fix height and width as well.

textView : leading to image, top space to label, trailing and bottom to container.

And keep using

tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension 

in your viewWillAppear()

Sign up to request clarification or add additional context in comments.

Comments

9

Update for swift 4.2

Use:

UITableView.automaticDimension 

Instead of:

UITableViewAutomaticDimension 

Comments

5

Make sure that the content mode is set to Scale To Fill of your UITextView Make sure that there are no height constraints for the UITextView and the card UIView

Try to add the estimated height into viewDidLoad:

override func viewDidLoad() { tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension } 

Maybe the AutoHeight is not working because of the UIView above the UITextView. Try to call the sizeToFit and layoutIfNeeded methods for the UIView in the cellForRowAt:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell cell.vwCard.sizeToFit() cell.vwCard.layoutIfNeeded() return cell } 

You can also try the sizeToFit and layoutIfNeeded as well for the UITextView.

Hope this works........

4 Comments

I have tried all of the above and even tried deleting the background card view but still it doesn't work. Any more ideas? really sorry @Abadii176
What is the Deployment Target of you application and which iOS version runs your simulator?
Deployment is 10.0 and sim is 10.3.1
What will happen if you set the delegate for heightForRowAt : override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100; }
0

Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)

Same type of example is explained here programmatically....

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.