Try to check more data not in cellForRowAtIndexPath but in UIScrollViewDelegate - [DataModel sharedMyLibrary] is my data source class loading video data using RESTful API with pagination, it's fetchWithCompletion method fetch data from server in async, it's hasMore method says that server has more data (JSON contains next link) LibraryTableViewController - is subclass of the UITableViewController, hasMore - is the view at the bottom of the table view in the storyboard containing a button, so user has two options: scroll down or press the button. Also if this view is visible indicates that there are more data on the server. _canFetch prevents nested loading from the server.
``
@interface LibraryTableViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *hasMore;
@end
@implementation LibraryTableViewController
{
__block volatile uint8_t _canFetch;
}
@synthesize hasMore = _hasMore;
- (void)viewDidLoad
{
_canFetch = 0x80;
[super viewDidLoad];
[self fetchVideos:NO];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGPoint offset = [scrollView contentOffset];
if ([[DataModel sharedMyLibrary] hasMore])
{
if (((velocity.y > 0.0) && (offset.y > (*targetContentOffset).y)) || ((velocity.x > 0.0) && (offset.x > (*targetContentOffset).x)))
{
[self fetchVideos:NO];
}
}
else
{
[_hasMore setHidden:YES];
}
}
- (IBAction)moreVideos:(UIButton *)sender
{
[self fetchVideos:NO];
}
- (IBAction)doRefresh:(UIRefreshControl *)sender
{
[sender endRefreshing];
[[DataModel sharedMyLibrary] clear];
[self fetchVideos:YES];
}
- (void)fetchVideos:(BOOL)reload
{
if (OSAtomicTestAndClear(0, &(_canFetch)))
{
__weak typeof(self) weakSelf = self;
[[DataModel sharedMyLibrary] fetchWithCompletion:^(NSArray *indexPathes) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
if (indexPathes != nil)
{
if (reload)
{
[[strongSelf tableView] reloadData];
}
else
{
[[strongSelf tableView] beginUpdates];
[[strongSelf tableView] insertRowsAtIndexPaths:indexPathes withRowAnimation:UITableViewRowAnimationAutomatic];
[[strongSelf tableView] endUpdates];
}
}
else
{
[[strongSelf tableView] reloadData];
}
[strongSelf->_hasMore setHidden:![[DataModel sharedMyLibrary] hasMore]];
strongSelf->_canFetch = 0x80;
});
}];
}
}
``