当内容适合屏幕时如何禁用 UITableView 表格中的滚动
我的 iphone 应用程序中有一些(分组样式)表格(仅在屏幕的一部分上,并且使用 Interface Builder 添加,而不是从 UITableViewController 子类化),其中 80%时间很小,适合屏幕。当表格适合屏幕时,我想禁用滚动,以使其更干净一些。但是,如果表格离开屏幕(稍后将行添加到其中时),我想再次启用滚动(因为否则您将看不到该内容。)
有没有办法做到这一点?我似乎无法弄清楚。我确实知道该怎么做:
tableView.scrollEnabled = NO;
但我不确定在哪里,或者我是否必须计算表对象大小或其他东西才能使其工作。
Update: Here's the solution that finally worked for me:
if (table.contentSize.height < table.frame.size.height) {
table.scrollEnabled = NO;
}
else {
table.scrollEnabled = YES;
}
我在表上调用 reloadData
后运行此代码,它计算出正确的大小并且似乎可以工作。
table.frame.size.height
是屏幕上显示的对象的实际大小(您可以在 Interface Builder
中看到它),而 table.contentSize。 height
是以下各项的高度:页眉、页脚以及每个单元格的高度之和。
I have a few (grouped style) tables in my iphone app (only on part of the screen and added with Interface Builder
though, not subclassed from UITableViewController
) that 80% of the time are small and will fit on the screen. When the table fits on the screen, I'd like to disable scrolling, to make it a bit cleaner. But if the table goes off the screen (when rows are later added to it), I'd like to enable scrolling again (because otherwise you can't see that content.)
Is there a way to do this? I can't seem to figure it out. I do know to do:
tableView.scrollEnabled = NO;
but I'm not sure where, or if I have to calculate the table object size or something to get this to work.
Update:
Here's the solution that finally worked for me:
if (table.contentSize.height < table.frame.size.height) {
table.scrollEnabled = NO;
}
else {
table.scrollEnabled = YES;
}
I run this code after calling reloadData
on the table, and it calculates the right sizes and appears to work.
table.frame.size.height
is the actual size of the object (you can see this in Interface Builder
) displayed on the screen, whereas table.contentSize.height
is the heights of: the header, the footer, and the height of every cell added together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我想你想设置
I think you want to set
在斯威夫特中:
In Swift:
您可以使用此函数验证可见单元格的数量:
此方法将返回一个包含可见单元格的数组,因此您可以计算此数组中的对象数量并与表中的对象数量进行比较。等于..您可以使用以下方法禁用滚动:
正如@Ginny提到的..我们可能会遇到部分可见单元格的问题,所以这个解决方案在这种情况下效果更好:
如果您使用autoLayout,这个解决方案可以完成这项工作:
You can verify the number of visible cells using this function:
This method will return an array with the cells that are visible, so you can count the number of objects in this array and compare with the number of objects in your table.. if it's equal.. you can disable the scrolling using:
As @Ginny mentioned.. we would can have problems with partially visible cells, so this solution works better in this case:
In case you are using autoLayout this solution do the job:
因此,有多个答案,并且需要同时包含所有内容,因此我添加了以下答案:
如果您使用自动布局,则通过设置该选项仅适合您:
tableView.alwaysBounceVertical = false
只需找到此选项并
取消勾选
“垂直弹跳
”选项即可。这是参考:
如果您不使用自动布局:
So there's are multiple answers and requires a all content at once place so I'm adding this answer:
If you're using AutoLayout, by setting this only should work for you:
tableView.alwaysBounceVertical = false
Just find this option and
untick
"Bounce Vertically
" option.Here's the reference:
If you're not using AutoLayout:
试试这个
try this
您可以通过在滚动视图区域中选择/取消选择这些来设置启用/禁用弹跳或滚动 tableview
You can set enable/disable bounce or scrolling the tableview by selecting/deselecting these in the Scroll View area
// 启用基于内容高度的滚动
self.tableView.scrollEnabled = table.contentSize.height >表.frame.size.height;
// Enable scrolling based on content height
self.tableView.scrollEnabled = table.contentSize.height > table.frame.size.height;
您可以在故事板中编辑它(如果您正在使用故事板)。在表格视图下有一个复选框,上面写着“滚动已启用”。取消选中它就完成了。
You can edit this in your storyboard (if you are using one). Under the table view there is a checkbox that says "Scrolling Enabled". Uncheck it and you're done.
我认为 tableview 单元格的默认高度是 44.0f。您必须将数据源放在数组中吗?然后只需检查
[array count]*44.0f
是否超出框架边界,如果是,则设置tableview.scrollEnabled = NO
,否则将其设置为 YES。在您找出该特定表视图的数据源的地方执行此操作。The default height is 44.0f for a tableview cell I believe. You must be having your datasource in hand in a Array? Then just check if
[array count]*44.0f
goes beyond the frame bounds and if so settableview.scrollEnabled = NO
, else set it to YES. Do it where you figure the datasource out for that particular tableview.这对我有用。
This is what worked for me.