使UITableView不可滚动并调整高度以适应所有单元格

我在另一个UIScrollViewembedded了一个UITableView 。 我只想要UIScrollView滚动,而不是UITableView ,所以我想禁用在UITableView的滚动,以及扩大UITableViewcontentHeight ,以容纳一次dynamic。

我怎么去做这个?

设置tableView大小等于contentSize。

 override func viewDidLayoutSubviews() { tableView.frame.size = tableView.contentSize } 

您需要将UITableView滚动设置为false

 tableview.scrollEnabled = false; tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y, tableview.frame.size.width, tableview.frame.size.height+(resultArray.count*HEIGHT_OF_CELL)); 

然后,您可以通过将UITableView的内容大小设置为UITableView高度来在UIScrollView上添加UITableView

  1. 让你的tableview框架覆盖所有可能的行 – >没有滚动的表视图
  2. 设置scrollview的contentSize = tableView的大小。

尽pipe在scrollview中使用固定内容的tableview作为子视图是非常简单的,但你究竟在做什么呢? 具有固定内容以上和以下的dynamic单元格? 永久显示的内容与滑动部分在中心?

也许一个更清洁的devise将使用一个单独的UITableView与表头和页脚,或者一个页眉页脚利用UITableViewStyleGrouped提供的自动行为与UITableViewStylePlain

也就是说,其他答案似乎形成了一个体面的解决scheme:

 Set scroll enabled false on table view Set tableview content size to sum of all cell sizes Set tableview frame to content size Adjust scroll view frame to account for dynamic table size if necessary Add table view to scroll view amidst other content 

操作滚动行为的另一个select是检查委托scrollView shouldStartScrolling或类似方法中的tableview或scrollview,因为这将允许在UI控件上更灵活

  • 首先你需要设置UITableView滚动到NO
  • 其次,为表格视图创build一个height constraint ,并在调用[UITableView reloadData]更改height constraint

     tableViewHeightConstraint.constant = numberOfRows * heightOfEachRow; 

UITableView是一个滚动视图。 虽然你不应该嵌套UIScrollviews,你可以禁用滚动的表视图。