I need to implement horizontal pagination like google in iOS tableview. Is there any way we can do this using a single tableview? Could you suggest a way to do this?
Setting up for pagination in swift Let's create an Xcode project, add a table view in the Main storyboard. Create TableViewCell and Xib. After that, register TableViewCell and assign delegate and dataSource. Your TableViewCell.
The procedure to refine returned results using limit and offset parameters.
To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.
Overview. Table views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app's content. For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings ...
I'd suggest you to use a UICollectionView
instead of UITableView
. That being said, in order to load paginated data from your server you'll need to add a couple lines of code:
You need to add variables in your view controller in order to keep track of your data:
/**
* Set this flag when loading data.
*/
@property (nonatomic, assign) BOOL isLoading;
/**
* Set this flag if more data can be loaded.
*/
@property (assign, nonatomic) BOOL hasNextPage;
/**
* The current page loaded from the server. Used for pagination.
*/
@property (assign, nonatomic) int currentPage;
Depending on what implementation you use, there are the different methods to implement in which we'll check if it's the right time to load data or not.
And in your UITableViewDelegate
implementation add:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = tableView.contentOffset.y;
CGFloat height = tableView.contentSize.height - tableView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
}
And in your UICollectionViewDelegate
implementation add:
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = collectionView.contentOffset.y;
CGFloat height = collectionView.contentSize.height - collectionView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
}
Then to load the next page:
- (void)loadNextPage:(int)pageNumber {
if (self.isLoading) return;
self.isLoading = YES;
// Fetch your data here
// ..
// Once the request is finished, call this
self.isLoading = NO;
}
I would also suggest you to check for the scrolling direction and add it as a condition to load the next page. This answer explains how to perform that.
UITableView
is a subclass of a UIScrollView
. DO this in the UITableView
delegate:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 10;
if(y > h + reload_distance) {
NSLog(@"load more rows");
}
}
If you are getting data from a web service and if the service supports pagination then you can achieve this. if(y > h + reload_distance)
is true the make the call to load more data from web service and then reload table view. Hope this helps.. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With