开发者

How to embed UITableView into UIScrollView?

I made an UIScrollView with paging enabled (horizontal scrolling), and I want to embed several U开发者_开发知识库ITableView objects into the UIScrollView.

How to do that ? Is there any sample code ?

Thanks.


Below you will find the example that loads two UITableViewControllers as separate pages.

// .h File

@interface PagingViewController : UIViewController  <UIScrollViewDelegate>{

}

-(void)loadPage:(int)page;

@end



// .m File

static NSUInteger kNumberOfPages = 2;

@interface PagingViewController ()

@property(nonatomic,retain)NSMutableArray *viewControllers;
@property(nonatomic,retain)UIView *contentView;

@end

@implementation PagingViewController

@synthesize viewControllers;
@synthesize contentView;


-(id)init{

        self = [super init];

    if(self) {

        NSMutableArray *controllers = [[NSMutableArray alloc] init];
        for (unsigned i = 0; i < kNumberOfPages; i++)
        {
            [controllers addObject:[NSNull null]];
        }
        self.viewControllers = controllers;
        [controllers release];

        contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

        self.view = contentView;

        [self loadPage:0];

    }
    return self;
}

- (void)loadPage:(int)page{

    if (page < 0)
        return;
    if (page >= kNumberOfPages)
        return;
    if(page == 0){       
        FirstUITableViewController *firstTableVC = [viewControllers objectAtIndex:page];

        if ((NSNull *)firstTableVC == [NSNull null]){
            firstTableVC = [[FirstUITableViewController alloc] init];
            [viewControllers replaceObjectAtIndex:page withObject:firstTableVC];
            [firstTableVC release];
        }

        if (firstTableVC.view.superview == nil)
        {
            [contentView addSubview:firstTableVC.view];
        }
    }else if(page == 1){

        SecondUITableViewController *secondTableVC = [viewControllers objectAtIndex:page];

        if ((NSNull *)secondTableVC == [NSNull null]){
            secondTableVC = [[SecondUITableViewController alloc] init];
            [viewControllers replaceObjectAtIndex:page withObject:secondTableVC];
            [secondTableVC release];
        }

        if (secondTableVC.view.superview == nil)
        {
            [contentView addSubview:secondTableVC.view];
        }
    }
}

// Don't forget to dealloc


Edit:

You can have a lot of objects in your tableView, and self.tableView.scrollEnabled = YES will give a vertical scroll view to that.

To get a horizontal scroll I would suggest that you create a tableView for each view controller and add a UIPageControl with each page denoting a tableView.

Check the UIPageControl Documentation.

You can use this tutorial for development - http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜