开发者

How can I do vertical paging with UITableView?

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 http://img245.yfrog.com/img245/2073/pricethat.jpg 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜