简体   繁体   English

如何让scrollViewDidScrollToTop在UITableView中工作?

[英]How can I get scrollViewDidScrollToTop to work in a UITableView?

I think the title explains it all. 我认为标题解释了这一切。 I want to be notified when a user scrolls to the top of a tableview. 当用户滚动到tableview的顶部时,我希望收到通知。

I've tried the following with no luck and even added a UIScrollViewDelegate to the .h file. 我试过以下没有运气,甚至在.h文件中添加了UIScrollViewDelegate。

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    NSLog(@"ScrolledToTop");
}

Thanks! 谢谢!

Edit : I can get it to call if I press the status bar. 编辑 :如果按状态栏,我可以拨打电话。 But not if I scroll to the top. 但如果我滚动到顶部则不行。 Weird... could it have something to do with the tableview bouncing when it reaches the top? 很奇怪......它可能与桌面视图在到达顶部时弹跳有关吗?

What happens if you try this? 如果你试试这会怎么样?

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y == 0)
        NSLog(@"At the top");
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row == 0 && indexPath.section == 0 ) {
    NSLog(@"Showing top cell");

}

Is that close enough? 那够近吗?

The func scrollViewDidScroll(_ scrollView: UIScrollView) delegate method will do the trick but it gets called each time the scrollview moves a point. func scrollViewDidScroll(_ scrollView: UIScrollView)委托方法将完成这一操作,但每次scrollview移动一个点时都会调用它。 A less hyperactive alternative could be this: 一个不太活跃的替代方案可能是这样的:

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){
    if targetContentOffset.memory.y == 0.0 {
       println("Reached the Top")
}

It lets the delegate know the scrollView will reach the top before it actually gets there, and because targetContentOffset is a pointer you can change its value to adjust where the scrollview finishes its scrolling animation. 它允许委托知道scrollView将在它实际到达之前到达顶部,并且因为targetContentOffset是一个指针,您可以更改其值以调整scrollview完成其滚动动画的位置。

Two more options that might be simpler based on your situation are: 根据您的情况,可能更简单的另外两个选项是:

First, if you're trying to detect the result of an animation only, triggered either by animating contentOffset yourself or by making a call to UITableView scrollToRowAtIndexPath: , or UICollectionView scrollToItemAtIndexPath: , implement 首先,如果您尝试仅检测动画的结果,可以通过自己动画contentOffset或通过调用UITableView scrollToRowAtIndexPath:或UICollectionView scrollToItemAtIndexPath:触发,实现

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    // You're now at the end of the scrolling animation
}

This will not not be called after a manual scroll with your finger, which may or may not be helpful sometimes. 用手指手动滚动后不会调用此功能,有时可能有用,也可能没用。

Second, if you're trying to do something like infinite scrolling, where you refresh the list when you get to the top, or load another page of results when you get to the bottom, an easy way to do it is to just use make the call in your data source cellForRowAtIndexPath: which might save you the need to implement the delegate all together. 其次,如果你试图做无限滚动的事情,当你到达顶部时刷新列表,或者当你到达底部时加载另一页结果,一个简单的方法就是使用make您的数据源cellForRowAtIndexPath:的调用cellForRowAtIndexPath:这可能会节省您一起实现委托的需要。

Do it in Swift: 在Swift中做到:

func scrollViewDidScroll(scrollView: UIScrollView) {
    print(scrollView.contentOffset.y) 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM