How can I do vertical paging with UITableView?

Posted by vodkhang on Stack Overflow See other posts from Stack Overflow or by vodkhang
Published on 2010-05-18T08:09:02Z Indexed on 2010/05/18 8:10 UTC
Read the original article Hit count: 1150

Let me describe the situation I am trying to do:

I have a list of Items. When I go into ItemDetailView, which is a UITableView. I want to do paging here like: When I scroll down out of the table view, I want to display the next item. Like in Good Reader:

Paging examples Currently, I am trying 2 approaches but both do not really work for me. 1st: I let my UITableView over my scroll view and I have a nice animation and works quite ok except sometimes, the UITableView will receive event instead of my scroll view. And because, a UITableView is already a UITableView, this approach seems not be a good idea. Here is some code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width,   scrollView.frame.size.height * kNumberOfPages);

    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    [self loadScrollViewWithPage:0];
}

- (void)loadScrollViewWithPage:(int)page {
    if (page < 0) return;
    if (page >= kNumberOfPages) return;

 [self.currentViewController.view removeFromSuperview];
 self.currentViewController = [[[MyNewTableView alloc]initWithPageNumber:page] autorelease];

    if (nil == currentViewController.view.superview) {
        CGRect frame = scrollView.frame;
        [scrollView addSubview:currentViewController.view];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (pageControlUsed) {
        return;
    }

    CGFloat pageHeight = scrollView.frame.size.height;
    int page = floor((scrollView.contentOffset.y - pageHeight / 2) / pageHeight) + 1;
    [self loadScrollViewWithPage:page];
}
- (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;
    [self loadScrollViewWithPage:page];

 // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    [scrollView scrollRectToVisible:frame animated:YES];

    pageControlUsed = YES;
}

My second approach is: using pop and push of navigationController to pop the current ItemDetail and push a new one on top of it. But this approach will not give me a nice animation of scrolling down like the first approach.

So, the answer to how I can get it done with either approach will be appreciated

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone