如何使用PageControl在UIScrollView中添加UITableView

时间:2012-01-17 09:20:19

标签: iphone uitableview uiscrollview

在我的ViewController中,我有两个uitableview。其中一个是静态的,有一个部分和两个行,另一个是一个部分但有四行。 我想用UIScrollview进行UIPageControl,并且在每个页面中,我想添加第四个包含四行的tableView。但是scrollView中的页面数量可以更改。所以我尝试使用UILabel并且它可以工作但是使用tableView我看不到它。

我不知道你是否理解我的问题。我把我的循环代码。

for (int i = 1; i < [listAllContactDetails count] + 1; i++) 
 {
        UILabel *nomContact = [[UILabel alloc] initWithFrame:CGRectMake((i-1)*320, 20, 320, 30)];
        nomContact.backgroundColor = [UIColor yellowColor];
        nomContact.text = [[listAllContactDetails objectAtIndex:i-1] valueForKey:@"name"];
        [scroller addSubview:nomContact];
        [nomContact release];
 }

 scroller.delegate = self;
 scroller.contentSize = CGSizeMake(320*[listAllContactDetails count], 249);
 scroller.pagingEnabled = YES;

 pageControl.numberOfPages = [listAllContactDetails count];
 pageControl.currentPage = 0;

此代码使用UILabel,但不能使用UITableView。

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您应该能够通过在循环中设置框架,为每个页面添加UITableView,就像使用标签一样。

问题在于连接数据源。您需要将每个表视图的datasource属性绑定到循环内的视图控制器。然后根据循环索引为循环中的每个表提供不同的.tag属性。在您的数据源方法中,您需要检查tableview的标记以确定它是哪个页面。像这样:

for (int i = 0; i < [listAllContactDetails count]; i++) 
 {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i*320, 0, 320, 200) style:...]
        tableView.dataSource = self;
        tableView.tag = i + 1;
        [scroller addSubview:tableView];
        [tableView release];
 }

 scroller.delegate = self;
 scroller.contentSize = CGSizeMake(320*[listAllContactDetails count], 249);
 scroller.pagingEnabled = YES;

 pageControl.numberOfPages = [listAllContactDetails count];
 pageControl.currentPage = 0;

...

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
        NSInteger tag = tableView.tag;
        switch (tag)
        {
            case 1:
                return numberOfRowsForPage1;
            case 2:
                return numberOfRowsForPage2;
            etc...
        }
 }
相关问题