7

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?

bneely
  • 9,083
  • 4
  • 38
  • 46
abhhab
  • 253
  • 2
  • 6
  • 22
  • With a `UICollectionView`? – nhgrif Mar 06 '14 at 04:07
  • do you need pagination with vertical scroll also – Charan Giri Mar 06 '14 at 04:35
  • @CharanGiri I need only horizontal scrolling only so that each scroll will reload different data coming from backend. – abhhab Mar 06 '14 at 11:10
  • You should add more details to your question, what you're looking for is not clear. – jbouaziz Mar 07 '14 at 14:20
  • 1
    @jbouaziz Initially tableview has 10 records. However there are 100 records coming from backend. We can do vertical pagination in this tableview but is there any way so that the tableview can be swiped so that new data could be displayed in the same tableview. – abhhab Mar 10 '14 at 07:19
  • @jbouaziz: Minor point but edit reviews are not done by the OP but by experianced reviewers. We have no way to know if that's what the OP meant when we accept the change. – Deanna Mar 10 '14 at 14:50

4 Answers4

11

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.

UITableView

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];
}

UICollectionView

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.

Community
  • 1
  • 1
jbouaziz
  • 1,484
  • 1
  • 12
  • 24
4

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.. :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
1

You have 2 options for your requirement 1.Scrollview with pagination and creating UI design on it. 2. Tableview with transformation i mean horizontal tableview.

If you sure scrollview memory will be high when compared to tableview because you need to create many view on pagination. If you use tableview it will be easy to code and handle memory also.

Below is the code for transforming tableview.

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 will help you.. if you need any additional information let me know

Charan Giri
  • 1,097
  • 1
  • 9
  • 15
0

Where are you getting your data from? If you have an API layer you could build data paging into the API method. To reduce the amount of actual data that is being passed back and forth.

You can achieve this with a single tableView. You need to define your number of sections, and number of items per section.

Justin Donohoo
  • 380
  • 2
  • 14
  • I am getting data using MBOs from SMP 2.3. The incoming data contains 100 records and I need to show 20 records at a time. – abhhab Mar 06 '14 at 04:22
  • I am new to iOS.Can we do the horizontal scrolling using number of sections? – abhhab Mar 06 '14 at 04:26