ios 当内容适合屏幕时如何禁用 UITableView 表中的滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7410845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable scrolling in UITableView table when the content fits on the screen
提问by Ginny
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.)
我的 iphone 应用程序中有一些(分组样式)表(仅在屏幕的一部分上并添加了,Interface Builder
但不是从 中添加的UITableViewController
),它们 80% 的时间都很小并且适合屏幕。当表格适合屏幕时,我想禁用滚动,使其更干净一些。但是,如果表格离开屏幕(稍后将行添加到其中时),我想再次启用滚动(否则您将看不到该内容。)
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更新:这是最终对我有用的解决方案:
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.
我在调用reloadData
表后运行此代码,它计算出正确的大小并且似乎可以工作。
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.
table.frame.size.height
是Interface Builder
显示在屏幕上的对象的实际大小(您可以在 中看到),而table.contentSize.height
是高度:页眉、页脚和每个单元格的高度加在一起。
回答by Lily Ballard
I think you want to set
我想你想设置
tableView.alwaysBounceVertical = NO;
回答by Roberto Ferraz
You can verify the number of visible cells using this function:
您可以使用此函数验证可见单元格的数量:
- (NSArray *)visibleCells
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:
此方法将返回一个包含可见单元格的数组,因此您可以计算此数组中的对象数并与表中的对象数进行比较.. 如果相等.. 您可以使用以下方法禁用滚动:
tableView.scrollEnabled = NO;
As @Ginny mentioned.. we would can have problems with partially visible cells, so this solution works better in this case:
正如@Ginny 提到的......我们可能会遇到部分可见的单元格的问题,所以这个解决方案在这种情况下效果更好:
tableView.scrollEnabled = (tableView.contentSize.height <= CGRectGetHeight(tableView.frame));
In case you are using autoLayout this solution do the job:
如果您使用 autoLayout 这个解决方案做的工作:
tableView.alwaysBounceVertical = NO.
回答by Natividad Lara Diaz
In Swift:
在斯威夫特:
tableView.alwaysBounceVertical = false
回答by Hemang
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:
如果您使用的是 AutoLayout,通过设置它只对您有用:
- In code:
- 在代码中:
tableView.alwaysBounceVertical = false
tableView.alwaysBounceVertical = false
- or In Interface Builder:
- 或在界面生成器中:
Just find this option and untick
"Bounce Vertically
" option.
只需找到此选项和untick
“ Bounce Vertically
”选项。
Here's the reference:
这是参考:
If you're not using AutoLayout:
如果您不使用 AutoLayout:
override func viewDidLayoutSubviews() {
// Enable scrolling based on content height
tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
}
回答by codercat
try this
尝试这个
[yourTableView setBounces:NO];
回答by Akila Wasala
You can set enable/disable bounce or scrolling the tableview by selecting/deselecting these in the Scroll View area
您可以通过在 Scroll View 区域中选择/取消选择这些来设置启用/禁用反弹或滚动 tableview
回答by rjobidon
// Enable scrolling based on content height
self.tableView.scrollEnabled = table.contentSize.height > table.frame.size.height;
// Enable scrolling based on content height
self.tableView.scrollEnabled = table.contentSize.height > table.frame.size.height;
回答by ericmarkmartin
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.
您可以在故事板中编辑它(如果您正在使用)。在表格视图下有一个复选框,上面写着“启用滚动”。取消选中它,你就完成了。
回答by Bourne
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 set tableview.scrollEnabled = NO
, else set it to YES. Do it where you figure the datasource out for that particular tableview.
我相信 tableview 单元格的默认高度是 44.0f。您必须将数据源放在数组中吗?然后只需检查是否[array count]*44.0f
超出了框架边界,如果是tableview.scrollEnabled = NO
,则将其设置为 YES。在为该特定 tableview 找出数据源的地方执行此操作。