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

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

问题描述

我们的 UI 有一部分是一小部分标签,旁边有颜色样本.我接手的设计在布局中有六个这样的硬编码,即使实际数据是动态的,这意味着如果我们只需要显示三个,我们必须显式隐藏三个,这也会破坏页面的平衡.更糟糕的是,这些项目"中的每一个实际上都由几个子视图组成,因此一个包含六个硬编码项目的屏幕有十八个 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: UIView.noIntrinsicMetric, height: CGFloat(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天全站免登陆