How to make scrollViewDidScrollToTop work in UITableView?

I think the name explains all this. I want to receive notifications when the user scrolls to the top of the table.

I tried the following with no luck and even added a UIScrollViewDelegate to the .h file.

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

Thanks!

Change I can make him call if I press the status bar. But not if I scroll up. Strange ... could this have anything to do with dragging a table when it reaches the top?

+4
source share
5 answers

What happens if you try this?

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

Is it close enough?

+2
source

The func scrollViewDidScroll(_ scrollView: UIScrollView) delegate method func scrollViewDidScroll(_ scrollView: UIScrollView) does the trick, but it is called every time scrollview moves the point. A less hyperactive alternative may be the following:

 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 that the scrollView will reach the top before it gets there, and since targetContentOffset is a pointer, you can change its value to adjust where the scrollview ends its scroll animation.

+1
source

Two other options that may be easier depending on your situation:

Firstly, if you are trying to detect the result of only an animation, it starts either by animating the contentOffset yourself, or by calling UITableView scrollToRowAtIndexPath: or UICollectionView scrollToItemAtIndexPath:

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

This will not be called after manually scrolling with your finger, which sometimes may or may not be useful.

Secondly, if you are trying to do something like endless scrolling, where you update the list when you get the top, or load another page of results when you get to the bottom, an easy way to do this is to simply use a call to your cellForRowAtIndexPath: data cellForRowAtIndexPath: which can save you the need to combine a delegate.

0
source

Do it in Swift:

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

Source: https://habr.com/ru/post/1299051/


All Articles