16 Oct 2011

Pagination and Delegates in a UITableView

No Comments iOS

I recently needed to create a table view where the user could scroll to the bottom to load more data, similar to Facebook’s ‘pull to refresh’. This shows a loading indicator while more data is retrieved. To do this I used a delegate which is implemented by a custom table view cell.

The ViewController contains a method to load more data, which might be called when the user scrolls to the bottom. This finds the last cell and calls the toggleLoading method.
CustomTableViewController.h

-(void)toggleTableViewLoading:(bool)isLoading;

CustomTableViewController.m

-(void)toggleTableViewLoading {
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:[aroundMeArray count]+1 inSection:0];
    CustomTableViewCell *lastCell= [self.aroundMeTableView cellForRowAtIndexPath:lastIndexPath];
    [lastCell toggleLoading];
}

This method is declared in a delegate which the cell implements.

ActivityDelegate.h

@protocol ActivityDelegate <NSObject>
    @required
    -(void) toggleLoading:(bool)showLoading;
@end

The implementation can be blank.

The custom table cell loaded by your UITableViewController needs to implement this delegate so the UITableView knows is has the toggleLoading method.

In the UITableCell which implements ActivityDelegate, we put the method which shows/hides the indicator. This creates the loading spinner and adds/removes it from the accessoryView in the table cell.

ActivityTableViewCell.m

-(void) toggleLoading:(bool)showLoading {
    if(!showLoading) {
        [self.accessoryView setHidden:true];
        isLoading = false;
    } else {
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        spinner.frame = CGRectMake(0, 0, 24, 24);
        self.accessoryView = spinner;
        [spinner startAnimating];
        [spinner release];
        isLoading = true;
        [self.backgroundView setNeedsDisplay];
    }
}

ActivityTableViewCell.h declares that is has a loading indicator which can be toggled. Hook this up in Interface Builder.

#import <UIKit/UIKit.h>
#import "ActivityDelegate.h"
@interface ActivityTableViewCell : UITableViewCell<ActivityDelegate> {
    IBOutlet UIView *viwHaveData;
    IBOutlet UIView *viwNoData;
    IBOutlet UILabel *lblNoData;
    IBOutlet UIActivityIndicatorView *loadingIndicator;
    BOOL isLoading;
}
 
@property (nonatomic, retain) IBOutlet UIView *viwHaveData;
@property (nonatomic, retain) IBOutlet UIView *viwNoData;
@property (nonatomic, retain) IBOutlet UILabel *lblNoData;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, readwrite) BOOL isLoading;
 
@end

When you need to show the loading spinner, you can call [myCell toggleLoading];

Tags: , ,
written by
The author didn‘t add any Information to his profile yet.
No Responses to “Pagination and Delegates in a UITableView”

Leave a Reply