-3

I need to reduce the height of the UITableView custom cell, if their is no data for the optional labels to show. following image would be a great help to understand

enter image description here

I tried assigning constraints constant value to 0 and 20 for the pre/post Instruction's respective top/bottom on the availability of data, it's working perfect when both optional values (pre/post instructions) are available, but in case of missing anyvalue the cell is picking the minimum height see below

enter image description here

Altough in heightForRowAt method i'm returning

return UITableViewAutomaticDimension

If any of the optional value (On the yellow label) is missing, I need to manage the exterior space of that label in the cell, Please suggest me a better solution

Mohit G.
  • 1,157
  • 2
  • 12
  • 22

1 Answers1

1

you can achieve this using a UIStackView.

Just embbed the Pre Instructions, Ingredient Name and Post Instruction inside a Vertical UIStackView, then add the constrains, 20 from top and bottom, whatever you want trailing and the spacing you want from the left label. And finally add the item spacing you want between the UIStackView items (4 o 8 pts for example).

Then, what you need to do is just hide the label when it has no content and the UIStackView will do the magic for you, it will handle the spacing when an item is hidden.

;)

Esteban Vallejo
  • 697
  • 1
  • 5
  • 11
  • thank you @Esteban for your opinion, earlier I was trying this with stack view but wasn't able to manage the leading and top space, because all these labels are multiple lines as well (Dynamic content length) – Mohit G. Nov 21 '18 at 05:39
  • @MohitGarg just by setting constraints and priorities correctly will work for multiple multi-line labels, just have in mind which one will stretch (huggin) and compress, which will be centered on the cell (I think the left label will always be centered relative to the right "stack") and so. – Esteban Vallejo Nov 23 '18 at 15:45