【问题标题】:Detecting UITableView scrolling检测 UITableView 滚动
【发布时间】:2010-12-07 22:54:28
【问题描述】:

我将 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 何时向上或向下滚动?

【问题讨论】:

    标签: objective-c iphone cocoa-touch uitableview


    【解决方案1】:

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

    【讨论】:

    • viewDidScroll 只要您滚动,就会一直被调用。所以你不能在 scrollViewDidScroll 处将你的变量 isDragging 设置为 galse。
    • 是否可以区分向上或向下滚动?
    【解决方案2】:

    我想添加以下内容:

    如果您使用的是UITableView,则很可能您已经在实施UITableViewDelegate 来用数据填充表格。

    UITableViewDelegate 协议符合 UIScrollViewDelegate,因此您只需在 UITableViewDelegate 实现中直接实现 -scrollViewWillBeginDragging-scrollViewDidScroll 方法,如果实现类设置为 UITableView 的委托,它们将被自动调用.

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

    【讨论】:

      【解决方案3】:

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

      [mytableview reloadData];

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 2013-05-24
        • 2011-10-15
        • 1970-01-01
        • 2012-01-05
        • 1970-01-01
        相关资源
        最近更新 更多