检测UITableView滚动

问题描述:

我已经分类了UITableView(作为KRTableView)并实现了四种基于触摸的方法(touchesBegan,touchesEnded,touchesMoved和touchesCancelled),以便我可以检测基于触摸的事件何时在UITableView上处理。基本上我需要检测的是UITableView向上或向下滚动。检测UITableView滚动

但是,创建UITableView和创建上述方法的子类只能检测UITableViewCell中发生滚动或手指移动的情况,而不是整个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 

我怎么能当UITableView的是或滚动上下检测?

您不需要拦截事件方法。检查文档中的UIScrollViewDelegate协议,并根据您的情况实施-scrollViewDidScroll:-scrollViewWillBeginDragging:方法。

+0

viewDidScroll正在不断被只要你滚动调用。所以你不能在scrollViewDidScroll中设置变量isDragging为galse。 – 2013-03-28 07:19:56

+0

是否有可能在滚动或向下滚动之间进行区分? – Markus 2017-05-05 07:54:39

+0

@Markus https://stackoverflow.com/questions/2543670/finding-the-direction-of-scrolling-in-a-uiscrollview – tdios 2017-06-01 14:25:52

我想补充以下内容:

如果你是一个UITableView工作很可能你已经实施UITableViewDelegate填写表格与数据。

协议的UITableViewDelegate符合UIScrollViewDelegate,因此,所有你需要做的是落实直接在您的UITableViewDelegate实现-scrollViewWillBeginDragging-scrollViewDidScroll方法,如果实现类设置不如授人以你的UITableView,他们将被自动调用。

如果您还想拦截表格中的点击,而不仅仅是拖动和滚动,则可以扩展并实现您自己的UITableViewCell并使用其中的touchesBegan:方法。通过组合这两种方法,您应该能够在用户与UITableView交互时完成大部分所需的操作。

这是我的错误,我试图在重新加载tableview之前调用该方法。下面的代码帮助我解决了这个问题。

[mytableview reloadData];

[mytableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];