Search code examples
iosswiftuitableviewnslayoutconstraint

Adjust view's height to containing table view's row amount


I want to implement a dropdown containing a table view with dynamic cells, hence the height won't be static.

The table view is being updated when the user adds an item, since I don't want any white space, the view has to be adjusted - how can I adjust the view's height every time the user adds a new item?

In the beginning will be one item, hence there will be one row. I want the view, containing the table, to be only as high as the row is - now if the user adds another item, the view's height shall be adjusted to the amount of rows.


Solution

  • You need to hook the height of the table as outlet and whenever you add any new row re-adjust it with

     self.tableHeight.constant = rowHeight * arr.count
     self.view.layoutIfNeeded()