Poephoofd Lol
Poephoofd Lol

Reputation: 37

Label height in Cell won't change height dynamically

I have a cell with a few labels init. One of the label is for a question. My app has different questions with different lengths. So I need my label to have a dynamical height. I want to achieve this with auto layout. When I run my app with my current auto layout it doesn't change the height (See pictures) . I am not sure what I am doing wrong. I have changed the number of lines of the label to 0 and I have added:

tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableView.automaticDimension

and

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

to my viewDidLoad(), but it won't change the height of my label. The weird thing is that it will change the height when the view is changing to an other view. See thing Youtube link to see what I am talking about: Youtube video. Change the speed of the video to the slowest to see the best results. It's happening around 0:04 seconds into the video.

Constraints: Auto layout

AutoLayout

Label on device:

Label on device

Autor label Constraints:

Autor label

Bannerview Constraints:

Banner view contraints

Upvotes: 0

Views: 893

Answers (2)

Manish_Nainwal
Manish_Nainwal

Reputation: 301

add all constraint with respect to superview of that resizing label. also use

label.numberOfLines = 0
label.lineBreakMode = .ByWordWrapping 

Upvotes: 0

PGDev
PGDev

Reputation: 24341

Initially the tableViewCell take the height according to the label's text i.e. Loading...

Once you get Questions response from the Firebase, to reflect the changes in the tableViewCell's content and its height, you need to reload the whole tableView, i.e.

tableView.reloadData()

Call the above code once you receive the API response and after updating the dataSource that you're using for the tableView.

Upvotes: 1

Related Questions