Search code examples
iosswiftuitableviewuiscrollview

Add tableview to scrollview and have scrollview keep growing in height as cells are added to tableview


I want to make a full screen scrollview with various views in it but also have a tableview in it.

The thing is, I don't want the tableview to "reuse" views.

I am using auto-layout.

People keep asking why. Here is why:

I want the scrollview to keep growing in height as cells are added to the tableview. (using storyboard) This is something I want to do. Stop "suggesting" a "better" way because I know the "better" way. I am asking for this way.

For example:

enter image description here

As cells are added to the tableview, the entire scrollview keeps growing.


Solution

  • Hope you are using autolayout, considering this i guess you already added constraint from the bottom of tableview to the uiview below it and also add a height constraint to the tableview. Make a IBOutlet of this height constraint. now write a method that would calculate the height depending on number of items you have to show for the tableview like

    func calculateTableViewHeight() {
       heightConstraint.constant = YOUR_CELL_HEIGHT * NO_OF_ROWS
    }  
    

    Call this method after tableview.reloadData(). And also don't for get to set the scrolling for the tableview to false