Skip to content

Instantly share code, notes, and snippets.

@hramos
Created January 24, 2011 15:29
Show Gist options
  • Save hramos/793376 to your computer and use it in GitHub Desktop.
Save hramos/793376 to your computer and use it in GitHub Desktop.
Infinite Load More Table View Example
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if(_reachedEndOfFeed) return;
if(!_dynamicFeed) return;
DLog();
if (!reloading)
{
checkForRefresh = YES; // only check offset when dragging
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (reloading) return;
if(scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height)) {
if(delegate_ != nil && [delegate_ respondsToSelector:@selector(infiniteTableViewController:requestsNextTitlesForPage:)]) {
reloading=YES;
DLog(@"Will request more at page %d", (currentPage_ + 1));
[delegate_ infiniteTableViewController:self requestsNextTitlesForPage:(currentPage_ + 1)];
[self startLoadingAnimation];
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
if (reloading) return;
if(_reachedEndOfFeed) return;
if(!_dynamicFeed) return;
DLog(@"Scrollview offset y %f", scrollView.contentOffset.y);
DLog(@"Scrollview height %f", scrollView.contentSize.height);
DLog(@"Bounds height %f", self.view.bounds.size.height);
if(scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height * 2)) {
if(delegate_ != nil && [delegate_ respondsToSelector:@selector(infiniteTableViewController:requestsNextTitlesForPage:)]) {
reloading=YES;
DLog(@"Will request more at page %d", (currentPage_ + 1));
[delegate_ infiniteTableViewController:self requestsNextTitlesForPage:(currentPage_ + 1)];
[self startLoadingAnimation];
}
}
checkForRefresh = NO;
}
- (void) startLoadingAnimation {
if(loadingCell!=nil && activityView!=nil) {
[loadingCell.textLabel setText:@"Loading..."];
[activityView startAnimating];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopLoadingAnimations) name:kConnectivityErrorNotification object:nil];
id appDelegate = [[UIApplication sharedApplication] delegate];
if (appDelegate != nil && [appDelegate respondsToSelector:@selector(checkReachability)]) {
[appDelegate checkReachability];
}
}
}
- (void) stopLoadingAnimations {
if(loadingCell!=nil && activityView!=nil) {
[activityView stopAnimating];
[loadingCell.textLabel setText:@"Load more..."];
reloading = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self name:kConnectivityErrorNotification object:nil];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForLoadMoreAtIndexPath:(NSIndexPath *)indexPath {
static NSString *LoadingCellIdentifier = @"LoadingCell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
if (loadingCell == nil) {
loadingCell = [[[ClearLabelsCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadingCellIdentifier] autorelease];
loadingCell.backgroundView = [[[GradientView alloc] init] autorelease];
[loadingCell setSelectionStyle:UITableViewCellSelectionStyleBlue];
[loadingCell.textLabel setTextColor:[UIColor colorWithRed:55.0/255.0 green:90.0/255.0 blue:163.0/255.0 alpha:1.0]];
[loadingCell.textLabel setTextAlignment:UITextAlignmentCenter];
loadingCell.backgroundColor = [UIColor clearColor];
if([titles count] == 0) {
reloading = YES;
[loadingCell.textLabel setText:@"Loading..."];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopLoadingAnimations) name:kConnectivityErrorNotification object:nil];
id appDelegate = [[UIApplication sharedApplication] delegate];
if (appDelegate != nil && [appDelegate respondsToSelector:@selector(checkReachability)]) {
[appDelegate checkReachability];
}
}
else {
[loadingCell.textLabel setText:@"Load more..."];
reloading = NO;
}
loadingCell.textLabel.shadowColor = [UIColor whiteColor];
loadingCell.textLabel.shadowOffset = CGSizeMake(1, 1);
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(25.0f, 88.0f - 38.0f, 20.0f, 20.0f);
activityView.hidesWhenStopped = YES;
if([titles count] == 0)
[activityView startAnimating];
[loadingCell setAccessoryView:activityView];
[activityView release];
}
return loadingCell;
}
@hramos
Copy link
Author

hramos commented Jan 24, 2011

Added start/stop Loading Animation methods. They also have some additional code that listens for a notification that is sent from various other controllers (there is a reachability check up in the delegate, but our data access object also throws this notification if ASIHTTPRequest times out).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment