ios 在 UITableView 中禁用滚动(iPhone SDK 3.0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1237355/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:14:47  来源:igfitidea点击:

disable scrolling in a UITableView (iPhone SDK 3.0)

iosuitableviewscroll

提问by Mikkel R. Lund

I'm trying to disable scrolling in a UITableViewwhen editing a UITextFieldembedded in a UITableViewCell. This is just to prevent the cell from being scrolled out of sight when edited (and also to avoid some related cell "Recycling" problems). While googling around I've seen that somebody suggested the obvious:

我试图UITableView在编辑UITextField嵌入在UITableViewCell. 这只是为了防止单元格在编辑时滚动到视线之外(同时也是为了避免一些相关的单元格“回收”问题)。在谷歌搜索时,我看到有人提出了显而易见的建议:

tableView.scrollEnabled = NO:

or even

甚至

tableView.userInteractionEnabled = NO;

This does not work though (at least for me... iPhone SDK 3.0, tried on simulator) I set these properties to NO, I even check by logging that the properties are set to NO, but the UITableView keeps on responding normally to touch events. And it also happily scrolls. I wouldn't be that worried if somebody on the net were not claiming that this actually works.

虽然这不起作用(至少对我来说...... iPhone SDK 3.0,在模拟器上尝试过)我将这些属性设置为 NO,我什至通过记录属性设置为 NO 来检查,但 UITableView 继续正常响应触摸事件。它也愉快地滚动。如果网上有人没有声称这确实有效,我就不会那么担心。

Am I missing something? Or is the only alternative subclassing UITableView to make a functionality available in its superclass (UIScrollView) work again?

我错过了什么吗?或者是唯一的子类化 UITableView 以使其超类 (UIScrollView) 中的功能再次工作?

回答by Mikkel R. Lund

Did you try using self.tableView.scrollEnabled = NO;?

你试过用 self.tableView.scrollEnabled = NO;吗?

I've often tried that code from the web didn't work, simply because of a lack of the prefix self. I just tried this out without a problem.

我经常试过网络上的代码不起作用,仅仅是因为缺少前缀 self. 我刚刚试过这个没有问题。

I don't know if this work when turning it on and off dynamically. It does at least work for permanent settings when initializing the object...

我不知道这在动态打开和关闭时是否有效。在初始化对象时,它至少可以用于永久设置......

回答by David M.

If you're using UITableViewController, you also have a tableViewproperty, with no casting needed. This works for me:

如果您正在使用UITableViewController,则您还有一个tableView属性,无需进行强制转换。这对我有用:

self.tableView.scrollEnabled = NO;

Let me know if that works for you.

让我知道这是否适合您。

回答by SukruK

Did you try on storyboard unselect scrolling enabled?

您是否尝试启用故事板取消选择滚动?

Scrolling Enabled : NO

滚动启用:否

回答by Kof

None of these answers worked in my case. Table view kept scrolling ever though every scrollView was disabled.

这些答案都不适用于我的情况。尽管每个 scrollView 都被禁用,表视图一直在滚动。

Finally, I've found solution in here, claiming that UITableViewControllerdoes this "for me" whenever keyboard hides the UITextView being edit.

最后,我在这里找到了解决方案,声称UITableViewController每当键盘隐藏正在编辑的 UITextView 时,它都会“为我”这样做。

Solution is to inherit from UIViewControllerinstead of UITableViewControllerand implement the required table functionality myself.

解决方案是继承UIViewController而不是UITableViewController自己实现所需的表功能。

回答by DebugWithAnAccent

I tried:

我试过:

[(UIScrollView*)[self view] setScrollingEnabled:NO];

and it worked ([self view] is my view of the current view controller, i.e., a UITableView).

它起作用了([self view] 是我对当前视图控制器的看法,即 UITableView)。

The thing is, I get a warning:

问题是,我收到警告:

'UIScrollView' may not respond to '-setScrollingEnabled:'

“UIScrollView”可能不会响应“-setScrollingEnabled:”

In all honesty, the property is "scrollEnabled", but it works nonetheless with the aforementioned code!

老实说,该属性是“scrollEnabled”,但它仍然适用于上述代码!

So, the "right" way to do things, should be:

因此,“正确”的做事方式应该是:

[(UIScrollView*)[self view] setScrollEnabled:NO];

Why it also works the other way, is confusing me...

为什么它也以另一种方式工作,让我感到困惑......

回答by Manoj Mashru

if you want to scroll only if its content is not visible then set:

如果您只想在其内容不可见时滚动,则设置:

yourTableview.alwaysBounceVertical = NO;

yourTableview.alwaysBounceVertical = NO;

Here if your content is visible then your tableview will not scroll

在这里,如果您的内容可见,那么您的 tableview 将不会滚动