1

I'm trying to make the height of table view cell = the content of the cell.

I implemented these 2 lines in the viewDidLoad():

tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension

and here are the constraints in the cell

Still, the cell is changing its height!

2
  • What do you mean when you say the cell is changing its height? Commented Sep 18, 2017 at 8:27
  • I mean that the height is too small and it is behaving as if I did not add these 2 lines. The title is the only thing that is appearing in the cell. Commented Sep 18, 2017 at 8:31

4 Answers 4

1

TextView will not expand to fit the entire text by default because it has scrolling capabilities, for what you want you should disable scrolling in the textView.

Select the textView and in the Attributes Inspector tab scroll down and uncheck the "Scrolling Enabled"

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

2 Comments

excellent this worked for me!! but what to do for a cell that has an image, because I'm having the same problem.
@mahdi For an imageView based on how you need the image to be displayed you can either set a height constraint on it or set an aspect ratio constraint
0

It appears to me that your issue may be that the height of the UITextView is not explicitly stated. The natural behaviour of the text view is not to be a tall as it's content.

I would suggest adding a height constraint within interface builder, hooking it up to an outlet, and then within the cell layoutSubviews function calculating the height like so:

@IBOutlet var textViewHeightConstraint: NSLayoutConstraint!
func layoutSubviews() {
    super.layoutSubviews()
    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    textViewHeightConstraint.constant = newSize.height
}

Comments

0

try to use heightForRowAt indexpath

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {return UITableViewAutomaticDimension}

+

    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

1 Comment

or try this one to override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension }
0

In ViewDidLoad

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200

And put your both Title Label and Text Label (replace Text View) in a UIView. and give constraints to UIView
1. trailing, leading, bottom and top space as ZERO
2. give fixed hight as 200 and change relation as Greater than or equal (>=)

Then give constrains to Title label
1. trailing, leading and top space as ZERO
2. give fixed hight as 20 (your choice)

Give constrains to Text Label
1. trailing, leading, bottom and top space as ZERO
2. give fixed hight as 180 and change relation as Greater than or equal (>=)

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.