Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Swift: multi line labels in TableView rows

I'm trying to insert some multi-line label in a tableview cell, but it looks like in the image below. I would like the text wrapping at line break and occupying more lines as it needs, growing up the height of the cell if necessary.

In the image I would like that every label had the possibility to expand in multiple lines.

The label is embed in a vertical stackview that it's in turn embed in another stackview.

I've tried many many possibilities, including embedding every label in a view, setting 0 to label lines, and other things I found here on SO answers, but no, the labels are not wrapping correctly and they always occupy just one line.

enter image description here

EDIT:

I add a little explanation:

1) I think the problem is the stackview that it's setting automatically the height of the children views.

2) Yes, I can use dynamic height cells but that's not the point, cause for me it would be enough to have the cells all the same height, and when the text is not occupying two lines the cell will have more empty space under it. So my problem is just wrapping text of a label embed in a stackview

like image 970
Lorenzo Barbagli Avatar asked Aug 24 '17 13:08

Lorenzo Barbagli


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

It sounds like what you are looking for is dynamic cell height, there are a few things that need to happen for that to work:

1) set label lines = 0

2) set the tableview's estimatedRowHeight in viewDidLoad

3) Either after setting the estimatedRowHeight in viewDidLoad set the rowHeight to UITableViewAutomaticDimension or in heightForRow return UITableViewAutomaticDimension

4) Make sure that you do not have height constraints on the labels that you want to change height dynamically

Edited for updated question:

Since the label is in a stack view you want to make sure that the stack view does not have any height constraints and that it will adjust height as needed when the label expands.

like image 145
CSjunkie Avatar answered Dec 10 '22 17:12

CSjunkie