使用TableViewCells展开ScrollView



当tableViewCells被添加到tableview中以滚动整个视图而不是表时,我正在尝试扩展我的视图,但我增加tableview和ScrollView大小的实现并不完全有效,我不确定这是否与代码或约束集有关。我必须在故事板上设置什么样的限制才能使这项工作成功?

这是我尝试过的,它没有扩展滚动视图:

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

tableView.visibleCells.count与某个时间的单元格数量不同,您还需要使用自动布局约束,方法是给IB中的表一个默认高度,并将其挂起作为IBOutlet,然后在viewDidLayoutSubviews/viewDidAppear内执行

self.tableHeight.constant = numOcells * 120
self.view.layoutIfNeeded() 

最新更新