UITableView不可滚动,但高度足够 [英] UITableView not scrollable but enough height

查看:347
本文介绍了UITableView不可滚动,但高度足够的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a Storyboard where I have a Scroll View which contains a Table View. The first thing I did is to disable scrolling on the Table View, otherwise we'll get two nested scrollbars, erk ! But here is my problem, I want the Table View to have its height equals to the sum of its cells's height. ( Cells height are various, because they display comments ).

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

PS: For the dynamic cells height I used this :

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

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

Shortly: I want a Table View to behave like a height variable list.

推荐答案

可能还有其他方法可以做到这一点,但就我而言,我已经做到了,

There could be other ways also to do this, but in my case i have done this,

所以您要做的是,首先让tableView的所有单元格都加载,这样您就可以检查是否所有tableView单元格都按以下方式加载,

So what you have to do is, first let the tableView's all cells load so you can check is all tableView cell are loaded as below,

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,但是您需要scrollView.

And then after this you have to get the required height for your tableView and set this height to the tableView's height constraint to increase tableView's height to visible all cell without scrolling tableView but you would need to scrollView.

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的约束,那么它肯定会按您期望的那样工作...

That's it, if you have set your scrollView's constraints set correctly then it will definitely work as you are expecting...

更新

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

I have created a demo having dynamic cells, see the output below,

可滚动的TableView:

Scrollable TableView:

Scrollable ScrollView:

Scrollable ScrollView:

这篇关于UITableView不可滚动,但高度足够的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆