Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView infinite scrolling

How do I do an infinite scrolling in a UITableView? I know how to do it using a UIScrollView, in which apple has demonstrated in one of the WWDC's video. I tried doing the following in tableView:cellForRowAtIndexPath::

if (indexPath.row == [self.newsFeedData_ count] - 1)
{
    [self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];
    [self.tableView reloadData];
}

but this fails. Any other idea?

like image 966
adit Avatar asked May 01 '12 20:05

adit


2 Answers

If you need to know when you hit the bottom of the UITableView, become it's delegate (because it is a subclass of UIScrollView), and use the -scrollViewDidScroll: delegate method to compare the table's content height and it's actual scroll position.

EDIT (something like this):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView_ 
{   
    CGFloat actualPosition = scrollView_.contentOffset.y;
    CGFloat contentHeight = scrollView_.contentSize.height - (someArbitraryNumber);
    if (actualPosition >= contentHeight) {
        [self.newsFeedData_ addObjectsFromArray:self.newsFeedData_];
        [self.tableView reloadData];
     }
}
like image 147
CodaFi Avatar answered Oct 26 '22 03:10

CodaFi


You can support infinite scroll with pull to refresh at the top and/or scroll continuously at the bottom with a spinner wheel using:

https://github.com/samvermette/SVPullToRefresh

SVPullToRefresh handles the logic when UITableView reaches the bottom. A spinner is shown automatically and a callback block is fired. You add in your business logic to the callback block.

Here's an example:

#import "UIScrollView+SVInfiniteScrolling.h"

// ...

[tableView addInfiniteScrollingWithActionHandler:^{
    // append data to data source, insert new cells at the end of table view
    // call [tableView.infiniteScrollingView stopAnimating] when done
}];

This project can be added to your project using CocoaPods or directly compiled into your project.

like image 18
Richard H Fung Avatar answered Oct 26 '22 05:10

Richard H Fung