views:

252

answers:

1

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

+1  A: 

If you don't use any UITableView functionality (except for the scrolling), you can simply add custom views to an UIScrollView … and enable page scrolling.

unset
My uploaded code is just an experiment project out of my real project. But, in my working one, I have to use tableview because i have to list a list of comments for that item. I am not really familiar with all obj-C funcs so I prefer separate it out and do some experiments
vodkhang