We often need to add social features in our iPhone apps. Adding
features like commenting, shouts etc are very common. These type of data
are usually shown
with pagination with buttons like next, previous etc.
To reduce the overhead of creating this functionality again and again
we may encapsulate the common functions in a class. So did I for my
applications.
I have created a class named PaginationAgent. After loading records
it calls related methods of its delegate which should implement protocol
PaginationAgentDeleagte.
Additionally two php files are needed. One to return the total number
of records and another one is to fetch the page data. They must be
named respectively as
- get_total_number_of_records.php
- get_records.php
Lets see an example.
First, I have created a simple view based project “Pagination” and designed the view as u see:
Create a similar view using Interface Builder
1. Added a button to show previous page data
2. One button to reload the data.
3. Another to show data on next page.
4. Added a UITable which will show the result.
In PaginationViewController.h, imported PaginationAgent.h file and declared:
- PaginationAgent* objPagination;
PaginationAgent* objPagination;
Now lets see how to initialize it. In the viewDidLoad method I initialized this like this:
- objPagination = [[PaginationAgent alloc] init];
- objPagination.delegate = self;
- objPagination.remoteApiBasePath = @"http://your.server/path/to/folder";
objPagination = [[PaginationAgent alloc] init];
objPagination.delegate = self;
objPagination.remoteApiBasePath = @"http://your.server/path/to/folder";
remoteApiBasePath must be set to the location where the server side php files are placed.
No trailing slash.
PaginationAgentDelegate protocol declares the following functions to be overridden by the Delegate:
- /**
- * This method is called when the busystate is changed.
- * You can do something like disable/enable the buttons, grayout etc
- * based on the isBusy value.
- */
- -(void)paginationAgent:(PaginationAgent*)agent updateBusyMode:(BOOL)isBusy;
-
- /**
- * Called when total data count is received if shouldReloadAtFirstPage is set NO.
- */
- -(void)paginationAgent:(PaginationAgent*)agent totalDataCountReceived:(int)dataCount;
-
- /**
- * Called when page data is loaded. You may set your table dataSource here and realod
- * reload the table.
- */
- -(void)paginationAgent:(PaginationAgent*)agent pageDataReceived:(NSArray*)data;
-
- /**
- * Called if server sends malformed data
- */
- -(void)paginationAgent:(PaginationAgent*)agent onPaginationServerError:(NSString*)message;
-
- /**
- * Called if connection failed. It may occur due to poor or no internet connectivity
- */
- -(void)paginationAgent:(PaginationAgent*)agent onPaginationConnectionError:(NSString*)message;
/**
* This method is called when the busystate is changed.
* You can do something like disable/enable the buttons, grayout etc
* based on the isBusy value.
*/
-(void)paginationAgent:(PaginationAgent*)agent updateBusyMode:(BOOL)isBusy;
/**
* Called when total data count is received if shouldReloadAtFirstPage is set NO.
*/
-(void)paginationAgent:(PaginationAgent*)agent totalDataCountReceived:(int)dataCount;
/**
* Called when page data is loaded. You may set your table dataSource here and realod
* reload the table.
*/
-(void)paginationAgent:(PaginationAgent*)agent pageDataReceived:(NSArray*)data;
/**
* Called if server sends malformed data
*/
-(void)paginationAgent:(PaginationAgent*)agent onPaginationServerError:(NSString*)message;
/**
* Called if connection failed. It may occur due to poor or no internet connectivity
*/
-(void)paginationAgent:(PaginationAgent*)agent onPaginationConnectionError:(NSString*)message;
The php files must return data in json format and must use the format as used:
- 1,'message'=>'Success','data'=>33);
- echo json_encode($data);
-
- ?>
1,'message'=>'Success','data'=>33);
echo json_encode($data);
?>
- ...
- ...
- $data = array('success'=>1,'message'=>'Got data','data'=>$rows);
-
- echo json_encode($data);
...
...
$data = array('success'=>1,'message'=>'Got data','data'=>$rows);
echo json_encode($data);
See the attached project for details. The php files are inside the folder “PHPFILES”.
Pagination.zip