Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView的自动调整高度_Ios_Swift - Fatal编程技术网

Ios UITableView的自动调整高度

Ios UITableView的自动调整高度,ios,swift,Ios,Swift,我的ViewController中有多个tableview,每个tableview有不同的行数,请参见下图: 有没有办法根据行数匹配tableview的高度?您必须确定有多少个单元格,然后覆盖 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return CGRectGetHeight(self.tableView.bounds) /

我的ViewController中有多个tableview,每个tableview有不同的行数,请参见下图:


有没有办法根据行数匹配tableview的高度?

您必须确定有多少个单元格,然后覆盖

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  return CGRectGetHeight(self.tableView.bounds) / self.tableView.numberOfRowsInSection(indexPath.section)  
}

顺便说一句,我认为使用标题单元格比实现多个TableView更好。

在视图控制器中,放置类似以下内容的代码:

override func updateViewConstraints() {
    super.updateViewConstraints()
    tableViewHeightConstraint.constant = tableView.contentSize.height;
}
其中,tableViewHeightConstraint是@IBOutlet到NSLayoutConstraint。显然,对于多个表视图,您将需要引用多个高度约束,因此对于所需的每个表视图,updateViewConstraints中只有一行


这种技术的优点是它考虑了表视图的所有内容。它处理分组、自动单元格高度等问题。

我在TableView中得到了nil值上面的代码就是一个例子。你必须使它适应你的特殊情况。如果有三个表视图,则需要有三个@IBOutlets到这些表视图,三个@IBOutlets到三个相应的高度约束。然后您会说,tableView1HeightConstraint.constant=tableView1.contentSize.height等等。变量的名称将不同。我得到以下错误:警告:无法加载任何Objective-C类信息。这将大大降低可用类型信息的质量。lldb