检测的UITableView滚动(Detecting UITableView scrolling)

2019-08-20 07:20发布

我子类的UITableView(如KRTableView),并实施了四个基于触摸的方法(的touchesBegan,touchesEnded,touchesMoved,并touchesCancelled),以便当正在上一个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的是或滚动上下检测?

Answer 1:

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



Answer 2:

我想补充以下内容:

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

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

如果你也想拦截你的表格点击,不仅拖动和滚动,你可以扩展和实现自己的UITableViewCell和使用的touchesBegan:方法在那里。 通过这两种方法相结合,当用户与互动的UITableView,你应该能够做你最需要的东西。



Answer 3:

这是我的错误,我想重装tableview中之前调用该方法。 下面的代码帮我解决这个问题。

[mytableview reloadData];

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



文章来源: Detecting UITableView scrolling