Dynamic TableViewCell Height

I’m going to demonstrate on how I achieved the dynamic height for my UITableViewCell. Below is what it looks like.

This is a UITableViewCell for comments. ImageView on the left side is for the user’s avatar, the top most UILabel is for the username and lastly at the bottom is the UILabel for comment.

Yes don’t mind the red warnings, that is by design. The important here is for the UITableViewCell height to expand with AutoLayout.

First take note of the UIImageView’s constraints.

Next, the constraints for the username UILabel. As you may notice I did not specified a Height constraint, thus the red error for both UILabels.

And lastly the constraints for our comment UILabel.

Now in your code make sure you set the rowHeight and estimatedRowHeight to UITableView.automaticDimension

commentsTableView.rowHeight = UITableView.automaticDimension
commentsTableView.estimatedRowHeight = UITableView.automaticDimension

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

  You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

  You are commenting using your Twitter account. Log Out /  Change )

  You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s