简体   繁体   English

自行调整表格视图

[英]Self sizing tableview

First of all this is not a question about how to automatically size the cells inside the tableview, moreover how to automatically resize the entire tableview.首先这不是关于如何自动调整tableview内单元格大小的问题,而是如何自动调整整个tableview的大小。

So I have a scrollview which has a tableview and 2 other views as its subviews.所以我有一个滚动视图,它有一个 tableview 和 2 个其他视图作为它的子视图。 The tableview's cells already automatically resize itself, again this question is not about the individual cells. tableview 的单元格已经自动调整大小,同样这个问题与单个单元格无关。 However, the tableview does not resize at all.但是,tableview 根本不会调整大小。

What I have done: 1) Set up the tableview to have a top, bottom, leading and trailing constraint 2) Set the cells up to have auto layout enabled 3) * I do not know the cell size at build time 4) I have disabled scrolling mode on tableview我所做的:1) 将 tableview 设置为具有顶部、底部、前导和尾部约束 2) 将单元格设置为启用自动布局 3) * 我不知道构建时的单元格大小 4) 我有在 tableview 上禁用滚动模式

So long story short, how can I go along to get the tableview to resize itself?长话短说,我怎样才能让 tableview 调整自己的大小?

Edit编辑

The cells contain a label which can have various lines of text, so therefore the cells, which use auto layout, should then determine the height of the table view.单元格包含一个标签,可以有不同的文本行,因此使用自动布局的单元格应该确定表格视图的高度。

The following images show how the view is set up:下图显示了如何设置视图:

奎克尔奎克尔 2

As you can see the tableview is only a small part of the view and since the scrollview from the tableview is deactivated there should, and aren't, any scrolling problems.如您所见,tableview 只是视图的一小部分,并且由于 tableview 中的滚动视图已停用,因此应该也不会出现任何滚动问题。

EDIT 2编辑 2

This is actually how it should end however, i am calculating this on my own and everytime I want to make a small change to the cells the whole code, which calculates the height of the cell, needs to be rewritten and it is quite difficult for me to get the height just right.这实际上是它应该如何结束,但是,我自己计算这个,每次我想对单元格进行小的更改时,计算单元格高度的整个代码都需要重写,这对于我要得到恰到好处的高度。

Edit 3编辑 3

Until now I had a height constraint on the tableview which I calculated manually, however removing this constraint and trying to let auto layout handle the tableview height size creates the following error:到目前为止,我对手动计算的 tableview 有一个高度约束,但是删除此约束并尝试让自动布局处理 tableview 高度大小会产生以下错误:

Scroll View Need constraint for: Y position or height滚动视图需要约束:Y 位置或高度

I can conclude therefore that the tableview does not know how to automatically calculate the height based on its cells with autolayout.因此,我可以得出结论,tableview 不知道如何根据具有自动布局的单元格自动计算高度。

You don't need to create a height constraint or set frame whatsoever.您不需要创建高度约束或设置任何框架。 Create a subclass of UITableView and recalculate its intrinsicContentSize every time its contentSize changes aka new data added or removed.创建 UITableView 的子类,并在每次其contentSize更改(即添加或删除新数据)时重新计算其intrinsicContentSize contentSize Here is all you needed:这是您需要的一切:

class SelfSizingTableView: UITableView {
    override var contentSize: CGSize {
        didSet {
            invalidateIntrinsicContentSize()
            setNeedsLayout()
        }
    }

    override var intrinsicContentSize: CGSize {
        let height = min(.infinity, contentSize.height)
        return CGSize(width: contentSize.width, height: height)
    }
}

You can change your UITableView's frame by using tableview.frame = CGRect(x: <some_x>, y: <some_y>, width: <some_width>, height: <some_height>)您可以使用tableview.frame = CGRect(x: <some_x>, y: <some_y>, width: <some_width>, height: <some_height>)更改UITableView's框架

If your UITableViewCells use auto layout then they should resize when the UITableView's frame changes.如果您的UITableViewCells使用自动布局,那么它们应该在UITableView's框架更改时调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM