8

This is the scenario: I have a Table in which there are Cells with dynamic height. In each cell I have a label and a switch. When I run the App only these two can be seen but then I set the switch to true then a (hidden?) textview appears under these two elements making the height of the cell bigger.

How can I do this? Because when I try hidden elements take up place as well.

This is what I want: enter image description here

Reality: enter image description here

How can I do this? I tried using the new Stack View which can handle it fine, BUT NOT in a table cell....

How can I solve this?

nethuszar
  • 225
  • 3
  • 13

3 Answers3

1

I solved it mixing this answer: Dynamic Height Issue for UITableView Cells (Swift) for the dynamic height and then for the views that show or not I added a height constraint to the view and then on cellForRowAtIndexPath just set the constant of the constraint to 0 if you need to hide it or to your height if you show it.

Hope it helps!

Community
  • 1
  • 1
agfa555
  • 950
  • 13
  • 19
0

I think it could be solved using constraints:
- set the height constraint for your text view,
- create the outlet for that constraint in your view controller,
- change the constant property of the constraint in your view controller when the switch is used.

Macple
  • 41
  • 11
0

If you hide your text view, your text view is still there, text length is as long as it is not hidden.

So instead of hide/show the text of the text view, you'd better to update the text of your text view from "" -> "what ever you want to show."

In this case, the auto layout can handle your query fair easily. You only need the set

self.tableView.estimatedRowHeight = 65 //whatever number
self.tableView.rowHeight = UITableViewAutomaticDimension

and in the cell xib/storyboard, add constraints on thetop andbottom of your text view, don't also forget to make your text view not scrollable as well.

Actually, I recommend you to simply useUILabel in stead ofUITextField, you just need to set thelineNumber of theLabel to be 0, and you can neglect the scroll and size setting of the text view.

Developer Sheldon
  • 2,140
  • 1
  • 11
  • 17