ios UITableView 滚动事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8642699/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:02:46  来源:igfitidea点击:

UITableView Scroll event

iphoneobjective-ciosxcodeuitableview

提问by Alaa Eldin

I want to detect if mytable view has been scrolled, I tried all touch events like this one:

我想检测 mytable 视图是否已滚动,我尝试了所有这样的触摸事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
    [super touchesBegan:touches withEvent:event];
    //my code
  }

but it seems that all touch events don't response to scroll but they response only when cells are touched, moved,...etc

但似乎所有触摸事件都不响应滚动,但它们仅在单元格被触摸、移动等时响应

Is there a way to detect scroll event of UITableView ?

有没有办法检测 UITableView 的滚动事件?

回答by fabian789

If you implement the UITableViewDelegateprotocol, you can also implement one of the UIScrollViewDelegatemethods:

如果您实现了UITableViewDelegate协议,您还可以实现以下UIScrollViewDelegate方法之一:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

or

或者

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

For example, if you have a property called tableView:

例如,如果您有一个名为 的属性tableView

// ... setting up the table view here ...
self.tableView.delegate = self;
// ...

// Somewhere in your implementation file:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"Will begin dragging");
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"Did Scroll");
}

This is because UITableViewDelegateconforms to UIScrollViewDelegate, as can be seen in the documentation or in the header file.

这是因为UITableViewDelegate符合UIScrollViewDelegate,可以在文档或头文件中看到。

回答by dev

If you have more than one table views as asked by Solidus, you can cast the scrollview from the callback to tableview as UITableView is derived from UIScrollView and then compare with the tableviews to find the source tableview.

如果 Solidus 要求您有多个表视图,则可以将滚动视图从回调转换为表视图,因为 UITableView 是从 UIScrollView 派生的,然后与表视图进行比较以找到源表视图。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {    
        UITableView* fromTableView = (UITableView*) scrollView;
        UITableView* targetTableView = nil;
        if (fromTableView == self.leftTable) {
            targetTableView = self.leftTable;
        } else {
            targetTableView = self.rightTable;
        }
...
}

回答by pableiros

These are the methods from UITableViewDelegatefor Swift 3 to detect when an UITableViewwill scroll or did scroll:

这些是UITableViewDelegateSwift 3 中用于检测何时UITableView将滚动或确实滚动的方法:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {

}