Determine if UITableView is near bottom when scrolling

Here's my way of finding out if UITableView is near bottom when scrolling. And when it does, we fetch and load another batch of items.

In my implementation of SomeTableViewController, the number of items to show matches the number of sections. So turns out, one row per section (this is because I intend to use the SectionHeaderView of each section).

Note that UITableViewController, by itself, conforms to UITableViewDelegate.

SomeTableViewController.h
...
@interface SomeTableViewController : UITableViewController {
  NSMutableArray * _tableViewData;
}
@end
...


SomeTableViewController.m
...
#pragma mark -
#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  // If last section is about to be shown...
  if ([indexPath section] == [_tableViewData count] - 1) {
    NSLog(@"...start fetching more items.");
  }
}
...

Bookmark and Share

1 comments :

JP Yao said...

Use this instead to get rid of your mutable array.

if ((indexPath.section == [self.tableView numberOfSections] - 1) && (indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1))

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)