Sheehan Alam
Sheehan Alam

Reputation: 60879

Problem detecting if UITableView has scrolled to the bottom

I'm using the following code to detect if I've reached the bottom of a UITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) {
        NSLog(@"bottom!");
        NSLog(@"%@", [self getLastMessageID]);
        [self getMoreStuff:[self getLastMessageID]];
    }

}

This works fine, but the only problem is when the user is pulling the tableview down (like pull to refresh) the code fires. How can I handle this?

Upvotes: 9

Views: 9184

Answers (3)

SriPriya
SriPriya

Reputation: 1240

try this way

    if(self.tableview.contentOffset.y<0){
              //it means table view is pulled down like refresh
              return;
            }
else if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) {
        NSLog(@"bottom!");
        NSLog(@"%@", [self getLastMessageID]);
        [self getMoreStuff:[self getLastMessageID]];
    }

Upvotes: 19

Denis Kutlubaev
Denis Kutlubaev

Reputation: 16124

Try to use another algorithm to detect, that you reached the bottom of table view. For example, look at the cellForRowAtIndexPath:(NSIndexPath *)indexPath to the indexPath of a current cell and if the number of row in IndexPath equals to the number of rows in your data array, then you reached a bottom.

Upvotes: 0

Joris
Joris

Reputation: 6296

Store the last position of the scrollview in your didScroll method. If you detect a scroll down and the last position was already the bottom of the scrollview then you ignore it.

Upvotes: 0

Related Questions