0
votes

I am trying to implement a scrollView that contains a text field, button, and table view. I am completely lost trying to get the thing to scroll, I've tried changing the height of UITableView but that has not worked, I've tried adjusting the height of the content View and scrollView, but that has not worked either. How can I get the scrollview to scroll and adjust the size when another tableViewCell is added to the tableView?

override func viewDidLayoutSubviews() {
        
        tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: CGFloat(tableView.numberOfRows(inSection: 0) * 120))
        tableView.reloadData()

    }


@IBAction func addExercisePressed(_ sender: Any) {
    test.append("nice")
    tableView.reloadData()
    
    tableView.frame = CGRect(x: tableView.frame.origin.x,y: tableView.frame.origin.y ,width: self.view.frame.size.width, height: CGFloat(test.count * 120))
    scrollView.frame.size = CGSize(width: self.view.frame.size.width, height: tableView.frame.size.height)
    
 
}

enter image description here

2
Why tableview inside a scrollview? You can use a single tableview to achieved same layout without any problem you mentionaiwiguna
How so, I want buttons to scroll with the view, is that possible in a tableView?Noah Iarrobino
Use tableheaderviewaiwiguna
much appreciatedNoah Iarrobino

2 Answers

1
votes

Please dont use tableView inside a scrollView, tableView is subclass of scrollView and that's mean tableView already have scroll function

your answer to set

tableHeight.constant = tableView.contentSize.height

It will make recycle/dequeue cell function useless because tableView will show all the cell immediately, tableview will call cellForRowAt for all indexes that showed in tableview viewport

UITableview inside Scrollview call cellforrow too many times

Here why we use dequeueReusableCellWithIdentifier

From what I see you want something like this right

enter image description here

you can check this repo how I only use a tableview to achieved that

please note that tableHeaderView is scrollable, sectionHeaderView is sticky

0
votes

All I needed was to set a height constraint for the TableView and connect that via IBOutlet, then this line of code wherever reloading table data is needed

tableHeight.constant = tableView.contentSize.height