I want to implement pagination in iOS tableview

I need to implement horizontal pagination like google in iOS tableview. Is there a way to do this using a single table? Could you suggest a way to do this?

+7
ios uitableview pagination
source share
4 answers

I suggest you use a UICollectionView instead of a UITableView . At the same time, to download paginated data from your server, you need to add a couple of lines of code: You need to add variables to your view controller in order to track 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 which implementation you use, there are various methods for the implementation in which we will check whether the data is loaded correctly or not.

UITableView

And in your implementation of UITableViewDelegate 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]; } 

UICollectionView

And in your implementation of UICollectionViewDelegate 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 following 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 also suggest that you check the scroll direction and add it as a condition to load the next page. This answer explains how to accomplish this.

+10
source share

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 get data from a web service, and if the service supports pagination, you can achieve this. if(y > h + reload_distance) true, make a call to load more data from the web service, and then reload the table view. Hope this helps .. :)

+3
source share

You have 2 options for your 1.Scrollview requirement with pagination and UI design. 2. Table with conversion i means horizontal table.

If you are sure that the scroll memory will be high compared to the table view, because you need to create many views for pagination. If you use tableview, it will be easy to encode and process memory.

The following is the tableview conversion code.

 UITableView* menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStylePlain]; menuTableView.delegate=self; menuTableView.dataSource=self; CGAffineTransform transform = CGAffineTransformMakeRotation(-1.5707963); menuTableView.transform = transform; // Repositions and resizes the view. CGRect contentRect = CGRectMake(0, 90, 320, 300); menuTableView.frame = contentRect; menuTableView.pagingEnabled= YES; menuTableView.separatorColor=[UIColor grayColor]; [self.view addSubview:menuTableView]; 

Hope this helps you .. if you need more info let me know

+1
source share

Where do you get your data from? If you have an API level, you can create data paging in the API method. To reduce the amount of actual data that is transferred back and forth.

You can achieve this with a single tableView. You need to determine the number of sections and the number of elements in the section.

0
source share

All Articles