Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell if UITableView has scrolled to top

I tried this:

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView 

But it didn't fire when I scrolled the table view to the top.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 

Does fire so the delegate isn't the problem.

In viewDidLoad I also set [myTbl setDoesScrollToTop:YES];

like image 515
JoshDG Avatar asked Apr 02 '13 19:04

JoshDG


People also ask

How to scroll to top UITableView swift?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

How can I tell if a tableView is scrolling in Swift?

Since a scrollView has a panGesture we can check the velocity of that gesture. If the tableView was programmatically scrolled the velocity in both x and y directions is 0.0. By checking this velocity we can determine if the user scrolled the tableView because the panGesture has a velocity. Save this answer.

Is UITableView scrollable?

Scrolling to a certain point in a table view is a common requirement. For example, you might scroll to show new entries after loading new data or to show that a row has updated data. Since UITableView inherits from UIScrollView it's easy to scroll programmatically.


2 Answers

The scrollViewDidScrollToTop: method fires when the user clicks on the status bar and the scrollsToTop property is set to YES. From the docs:

The scroll view sends this message when it finishes scrolling to the top of the content. It might call it immediately if the top of the content is already shown. For the scroll-to-top gesture (a tap on the status bar) to be effective, the scrollsToTop property of the UIScrollView must be set to YES.

It does not fire if the user manually scrolls to the top. If you want to handle this case you will have to implement the scrollViewDidScroll: method and check to see whether the scroll is at the top yourself.

You can check this through the contentOffset property e.g.:

if (scrollView.contentOffset.y == 0) { // TOP } 
like image 124
Elliott James Perry Avatar answered Sep 22 '22 19:09

Elliott James Perry


When the table view goes under navigation bar and safe area layout guides are enabled, the following check can be done:

if (tableView.contentOffset.y + tableView.safeAreaInsets.top) == 0 { ... } 

Bonus: Check for contentSize if you want to avoid getting 0 before the content is load:

if tableView.contentSize.height > 0 &&      ((tableView.contentOffset.y + tableView.safeAreaInsets.top) == 0) { ... } 
like image 41
naydin Avatar answered Sep 21 '22 19:09

naydin