UITableView内容高度
我有一个 UITableView 设置为不启用滚动,并且它存在于 UIScrollView 中。我这样做是因为设计规范要求看起来像表格视图的东西(实际上有两个并排),并且实现表格视图比添加一大堆按钮要容易得多,(分组表视图)。
问题是,我需要知道滚动视图的容器视图有多大,以便它滚动表视图的整个高度。加载后,有什么方法可以找到桌面视图的高度吗?没有像滚动视图这样的 contentView 属性,框架似乎是静态的等等......
有什么想法吗?
I have a UITableView that is set to not enable scrolling, and it exists in a UIScrollView. I'm doing it this way as the design specs call for something that looks like a table view, (actually there are two of them side by side), and it would be much easier to implement tableviews rather than adding a whole bunch of buttons, (grouped table views).
Question is, I need to know how big to make the container view for the scrollview, so it scrolls the whole height of the table views. Once loaded, is there any way to find the height of a tableview? There is no contentView property like a scroll view, frame seems to be static, etc...
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
然后您可以使用 contentHeight 变量来设置滚动视图的 contentSize。
Use
You can then use the contentHeight variable to set the contentSize for the scrollView.
一个更适合我的通用解决方案:
除了安德烈的解决方案之外,它还考虑了空白部分和部分页脚。
A more general solution that works for me:
In addition to Andrei's solution, it accounts for empty sections and section footers.
UITableView
是UIScrollView
的子类,因此它有一个contentSize
属性,您应该能够使用它没有问题:但是,如 几个 其他 SO问题指出,当您对表视图进行更新(如插入行)时,其
contentSize
似乎不会像 UIKit 中大多数其他动画调整大小那样立即更新。在这种情况下,您可能需要求助于迈克尔·曼纳的答案。 (尽管我认为在UITableView
上作为一个类别实现更有意义)UITableView
is a subclass ofUIScrollView
, so it has acontentSize
property that you should be able to use no problem:However, as several other SO questions have pointed out, when you make an update to a table view (like inserting a row), its
contentSize
doesn't appear to be updated immediately like it is for most other animated resizing in UIKit. In this case, you may need to resort to something like Michael Manner's answer. (Although I think it makes better sense implemented as a category onUITableView
)您可以遍历这些部分并使用 rectForSection 来计算总高度(这也包括页脚和页眉!)。我很快在
UITableView
上使用了以下扩展You can run over the sections and use the
rectForSection
to calculate the total height (this included footer and header as well!). In swift I use the following extension onUITableView