objective-c 检测 UITableView 滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1587855/
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
Detecting UITableView scrolling
提问by Kishyr Ramdial
I've subclassed UITableView (as KRTableView) and implemented the four touch-based methods (touchesBegan, touchesEnded, touchesMoved, and touchesCancelled) so that I can detect when a touch-based event is being handled on a UITableView. Essentially what I need to detect is when the UITableView is scrolling up or down.
我已经继承了 UITableView(作为 KRTableView)并实现了四种基于触摸的方法(touchesBegan、touchesEnded、touchesMoved 和 touchesCancelled),以便我可以检测何时在 UITableView 上处理基于触摸的事件。基本上我需要检测的是 UITableView 何时向上或向下滚动。
However, subclassing UITableView and creating the above methods only detects when scrolling or finger movement is occuring within a UITableViewCell, not on the entire UITableView.
但是,子类化 UITableView 并创建上述方法仅检测 UITableViewCell 内何时发生滚动或手指移动,而不是整个 UITableView。
As soon as my finger is moved onto the next cell, the touch events don't do anything.
只要我的手指移到下一个单元格上,触摸事件就不会做任何事情。
This is how I'm subclassing UITableView:
这就是我继承 UITableView 的方式:
#import "KRTableView.h"
@implementation KRTableView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touches began...");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
NSLog(@"touchesMoved occured");
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
NSLog(@"touchesCancelled occured");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSLog(@"A tap was detected on KRTableView");
}
@end
How can I detect when the UITableView is scrolling up or down?
如何检测 UITableView 何时向上或向下滚动?
回答by NSResponder
You don't need to intercept the event methods. Check the docs for the UIScrollViewDelegateprotocol, and implement the -scrollViewDidScroll:or -scrollViewWillBeginDragging:methods as appropriate to your situation.
您不需要拦截事件方法。检查UIScrollViewDelegate协议的文档,并根据您的情况实施-scrollViewDidScroll:或-scrollViewWillBeginDragging:方法。
回答by jake_hetfield
I would like to add the following:
我想添加以下内容:
If you are working with a UITableViewit is likely that you are already implementing the UITableViewDelegateto fill the table with data.
如果您正在使用 aUITableView很可能您已经在实施UITableViewDelegate用数据填充表格。
The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDraggingand -scrollViewDidScrolldirectly in your UITableViewDelegate implementation and they will be called automatically if the implementation class is set as delegate to your UITableView.
UITableViewDelegate 协议符合 UIScrollViewDelegate,所以你需要做的就是直接在你的 UITableViewDelegate 实现中实现这些方法-scrollViewWillBeginDragging,-scrollViewDidScroll如果实现类被设置为你的 UITableView 的委托,它们将被自动调用。
If you also want to intercept clicks in your table and not only dragging and scrolling, you can extend and implement your own UITableViewCell and use the touchesBegan: methods in there. By combining these two methods you should be able to do most of the things you need when the user interacts with the UITableView.
如果您还想拦截表格中的点击,而不仅仅是拖动和滚动,您可以扩展和实现自己的 UITableViewCell 并使用其中的 touchesBegan: 方法。通过结合这两种方法,当用户与 UITableView 交互时,你应该能够做大部分你需要的事情。
回答by Nassif
It was my mistake, I tried to call the method before reloading the tableview. The following code helped me solve this issue.
这是我的错误,我尝试在重新加载 tableview 之前调用该方法。以下代码帮助我解决了这个问题。
[mytableview reloadData];
[mytableview reloadData];
[mytableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[mytableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

