disable scrolling in a UITableView (iPhone SDK 3.0

2019-01-23 12:41发布

I'm trying to disable scrolling in a UITableView when editing a UITextField embedded 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:

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.

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

6条回答
2楼-- · 2019-01-23 13:01

Did you try on storyboard unselect scrolling enabled?

Scrolling Enabled : NO

查看更多
放我归山
3楼-- · 2019-01-23 13:05

Did you try using 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.

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...

查看更多
萌系小妹纸
4楼-- · 2019-01-23 13:09

I tried:

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

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

The thing is, I get a warning:

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

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

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

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

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-23 13:21

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

yourTableview.alwaysBounceVertical = NO;

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

查看更多
疯言疯语
6楼-- · 2019-01-23 13:22

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

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

Solution is to inherit from UIViewController instead of UITableViewController and implement the required table functionality myself.

查看更多
Ridiculous、
7楼-- · 2019-01-23 13:23

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

self.tableView.scrollEnabled = NO;

Let me know if that works for you.

查看更多
登录 后发表回答