Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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_Xcode_Autolayout - Fatal编程技术网

Ios UITableView不可滚动,但高度足够

Ios UITableView不可滚动,但高度足够,ios,swift,xcode,autolayout,Ios,Swift,Xcode,Autolayout,我正在构建一个故事板,其中有一个包含表视图的滚动视图。 我做的第一件事是禁用表视图上的滚动,否则我们将得到两个嵌套的滚动条,erk! 但这是我的问题,我希望表视图的高度等于其单元格高度之和。(单元格高度不同,因为它们显示注释) PS:对于动态单元格高度,我使用了: self.commentsTableView.estimatedRowHeight = 90.0 self.commentsTableView.rowHeight = UITableViewAutomaticDimension 简而

我正在构建一个故事板,其中有一个包含表视图的滚动视图。 我做的第一件事是禁用表视图上的滚动,否则我们将得到两个嵌套的滚动条,erk! 但这是我的问题,我希望表视图的高度等于其单元格高度之和。(单元格高度不同,因为它们显示注释)

PS:对于动态单元格高度,我使用了:

self.commentsTableView.estimatedRowHeight = 90.0
self.commentsTableView.rowHeight = UITableViewAutomaticDimension

简而言之:我希望一个表视图的行为类似于一个高度变量列表。

也可以有其他方法来做到这一点,但在我的例子中,我已经做到了

所以你要做的是,首先让tableView的所有单元格加载,这样你就可以检查是否所有tableView单元格都加载了,如下所示

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    //Update tableView Offset
    if indexPath.row() == (tableView.indexPathsForVisibleRows.last! as! NSIndexPath).row {
        //End of loading all Visible cells
        self.updateTableViewOffsetForTableView(self.tableView, withHeightConstraint: self.heightConstraintTableView)
        //If cell's are more than 10 or so that it could not fit on the tableView's visible area then you have to go for other way to check for last cell loaded 
    }
}
然后,您必须获得tableView所需的高度,并将此高度设置为tableView的高度约束,以将tableView的高度增加到可见的所有单元格,而不滚动tableView,但您需要滚动tableView

func updateTableViewOffsetForTableView(tableView: UITableView, withHeightConstraint heightConstraintToBeUpdated: NSLayoutConstraint) {

    var heightRequiredForTable: CGFloat = tableView.contentSize.height
    //Set some random height first, it is easy for tableView to shrink its height to high to low.
    heightConstraintToBeUpdated.constant = 300
    self.view!.layoutIfNeeded()
    self.view!.setNeedsUpdateConstraints()

    //Update the height constraint with height required for tableView 
    heightRequiredForTable = tableView.contentSize.height
    heightConstraintToBeUpdated.constant = heightRequiredForTable
    self.view!.layoutIfNeeded()
    self.view!.setNeedsUpdateConstraints()
}
就是这样,如果您已经正确设置了scrollView的约束集,那么它肯定会像您期望的那样工作

更新

我已经创建了一个具有动态单元格的演示,请参见下面的输出

可滚动的表格视图:

可滚动滚动视图:


谢谢,但我不工作,因为contentSize.height返回行数*EstimatedRowhight,但我的行高度是可变的。想法?不,contentSize不返回EstimatedRowhight,它返回tableView显示所有单元格所需的高度。我已经创建了具有动态高度单元的演示,它正在按预期工作。查看更新的答案。在使用XCode 11的iOS 13上,
contentSize
不会立即更新,而不会强制进行布局循环。iOS 13之前版本,
tableview.contentsize.height
返回正确的高度。但对于iOS 13,您必须调用
layoutifneed
以获得正确的
contentSize
。我仍然在寻找可以澄清UITableview上是否有任何布局周期更改的参考资料,但到目前为止我找不到任何。