UIScrollView 中 UITableView 的内容大小问题
我实际上使用界面生成器创建了一个 UIScrollView
,其中包含一个 UITableView
。我设置了 UIScrollView
的内容大小:
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
并将文件的所有者视图设置为 UIScrollView
我创建了一个 IBOutlet
UITableView< /code> 我在界面生成器中设置了它。
当视图加载时。它显示我的 UIScrollView
的正确内容大小。但它没有显示我的 UITableView
的正确内容大小。此外,当我更改 UIScrollView 的内容大小时,它也会更改 UITableView 的内容大小,就好像两个内容大小相关一样。
有人知道如何保持在界面生成器中设置的表格视图的内容大小吗?
I actually create a UIScrollView
with a UITableView
inside it with interface builder. I set the content size of my UIScrollView
with:
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
and I set my file's owner view to the UIScrollView
I created an IBOutlet
UITableView
and I set it in the interface builder.
When the view is loaded. it displays the correct content size for my UIScrollView
. But it doesn't display the correct content size for my UITableView
. Besides when I change the content size of my UIScrollView
, it changes the content size of my UITableView
as if both content size were related.
Anyone knows how can I keep the content size of my table view that I set in the interface builder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UITableView
是一个UIScrollView
。在极少数情况下,将UITableView
嵌入到UIScrollView
中会很有用。假设您确实想要这个,您需要在某处执行以下操作。可能在您的
UIViewController
中,例如viewDidAppear:
等方法中,或者在填充表视图后的某个地方。A
UITableView
is aUIScrollView
. There are very few times when it would be useful to embed aUITableView
inside aUIScrollView
.Assuming you really do want this, you'll need to do the following somewhere. Probably in your
UIViewController
in methods likeviewDidAppear:
or somewhere you after you populate the table view.动态管理 TableView 高度和 ScrollView 的示例,适用于 Swift:
参考:
Example to dynamically manage the TableView Height along with ScrollView, works in Swift:
References to:
如果您仍然遇到问题,请在不需要滚动视图的情况下尝试此操作。
在玩了一段时间 DBD 的示例后,我发现框架和 contentSize 似乎无法设置在一起,如下所示:
尝试设置框架的最大高度,但保持滚动所有框架的能力如果存在大量细胞则含量。这个黑客似乎运作良好,并且如果需要的话可以根据更多条件进行修改:
这肯定有效。您可能需要调整 .xib 或代码中的 autoresizingMask,但此 hack 是我发现的唯一解决方案,可以处理我的案例中的所有不同变量。
If you're still having problems, try this without the need for a scrollView.
After playing around with DBD's example for awhile, I found that the frame and contentSize don't seem to be able to be set together like:
The attempt was to set a max height of the frame but keep the ability to scroll though all of the content if there were a large number of cells. This hack seems to work well and can be modified with more conditions if needed:
This works for sure. You might need to adjust the autoresizingMask inside your .xib or in your code, but this hack is the only solution I found that takes care of all the different variables in my case.
多次调用下面的方法。每次你打电话都会有更多的细胞。
Call below method multiple times. Each time you call there will be more cells.