Hi Readers,
Pagination is required in most of our apps in today scenario. We can achieve it either pull to refresh from top or bottom. UIRefreshControl is the feature if you want to implement it from top side and insert processed records into top of your tableview. But these records can be added at bottom also. This can be implemented by defining delegate method of UITableView as:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// totalRecords is number of records which are loaded, initially it will be 0 and then added to datasource array which used to reload data.
// isLoading is a bool which gets update each time we hit to server and get the result. If we set it true while hitting server we don't hit again until we get the response.
if !(indexPath.row + 1 < self.totalRecords && self.isLoading == false) {
self.isLoading = true;
//getMoreRecords is method by which we get result from server for next page by passing totalRecords or page number.
self.getMoreRecords()
}
}
You can find it helping with comments added into code snippet. It simply imply the logic that we get the last cell when it is about to display by checking condition in code. It it was the last cell and we haven't request for records yet, we hit server for next page records.
Hope it helps. Thanks for reading.
1 Comment(s)