如果禁用滚动,是否可以根据显示的行数获取UITableView的固有内容大小以进行更新? [英] Can you get a UITableView's intrinsic content size to update based on the number of rows shown if scrolling is disabled?

查看:71
本文介绍了如果禁用滚动,是否可以根据显示的行数获取UITableView的固有内容大小以进行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的UI的一部分是一小部分标签,旁边有颜色样本.尽管实际数据是动态的,但我要接管的设计在布局中有六个硬编码,这意味着如果我们只需要显示三个,就必须显式地隐藏三个,这也浪费了页面的余量. .更糟糕的是,这些项目"中的每一个实际上都是由几个子视图组成的,因此一个包含六个硬编码项目的屏幕具有18个IBOutlets.

We have a portion of our UI which is a small list of labels with color swatches next to them. The design I'm taking over has six of these hard-coded in the layout even though the actual data is dynamic, meaning if we only need to show three, we have to explicitly hide three, which also throws off the balance of the page. Making matters worse is each one of those 'items' is actually made up of several sub-views so a screen with six hard-coded items has eighteen IBOutlets.

我想做的是改为使用UITableView代表屏幕的这一小部分,并且由于它不会滚动,所以我想知道是否可以使用AutoLayout配置内部内容的高度UITableView基于行数.

What I'm trying to do is to instead use a UITableView to represent this small portion of the screen, and since it won't scroll, I was wondering if you can use AutoLayout to configure the intrinsic content height of the UITableView to be based on the number of rows.

当前,我有一个测试页,其垂直限制在中心的UITableView,但是没有高度限制,因为我希望表格的固有内容大小能够反映可见的行.我还禁用了在桌子上滚动.重新加载表时,我称为updateConstraints.但是表仍然没有调整大小.

Currently I have a test page with a UITableView vertically constrained to the center, but without a height constraint because I am hoping to have the table's intrinsic content size reflect the visible rows. I have also disabled scrolling on the table. When I reload the table, I call updateConstraints. But the table still does not resize.

注意:我们不能使用UIStackView(这本来很完美),因为我们必须针对iOS8,并且直到iOS9才引入它,因此是此解决方案.

Note: We can't use a UIStackView (which would have been perfect for this) because we have to target iOS8 and that wasn't introduced until iOS9, hence this solution.

有人能做一些类似于我们需要的事情吗?

Has anyone been able to do something similar to our needs?

推荐答案

好,所以与UITextView不同,它看起来像UITableView都不基于可见行返回固有大小.但这不是通过子类实现的大问题,尤其是在只有一个部分,没有页眉或页脚并且行的高度固定的情况下.

Ok, so unlike UITextView, it doesn't look like UITableView ever returns an intrinsic size based on the visible rows. But that's not that big a deal to implement via a subclass, especially if there's a single section, no headers or footers, and the rows are of a fixed height.

class AutoSizingUiTableView : UITableView
{
    override func intrinsicContentSize() -> CGSize
    {
        let requiredHeight = rowCount * rowHeight
        return CGSize(width: -1, height: requiredHeight)
    }
}

我将由读者自己决定如何获得自己的rowCount.如果您具有可变的高度,多个部分等,则相同.您只需要更多的逻辑即可.

I'll leave it up to the reader to figure out how to get their own rowCount. The same if you have variable heights, multiple sections, etc. You just need more logic.

通过这样做,它可以很好地与AutoLayout一起使用.我只希望它能自动处理此问题.

By doing this, it works great with AutoLayout. I just wish it handled this automatically.

这篇关于如果禁用滚动,是否可以根据显示的行数获取UITableView的固有内容大小以进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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