Xcode UIScrollView中的2个UITableView

Xcode UIScrollView中的2个UITableView,xcode,uiscrollview,Xcode,Uiscrollview,下午好 我正在尝试创建一个包含两个表的滚动视图。代码张贴在下面,表格创建时没有问题,我正在构建时将数据加载到表格中,但第二个表格似乎工作正常,我无法滚动数据或选择单元格。有什么想法吗 谢谢 - (void)setupPage { scrollView.delegate = self; [self.scrollView setBackgroundColor:[UIColor grayColor]]; [scrollView setCanCancelContentTouches:NO];

下午好

我正在尝试创建一个包含两个表的滚动视图。代码张贴在下面,表格创建时没有问题,我正在构建时将数据加载到表格中,但第二个表格似乎工作正常,我无法滚动数据或选择单元格。有什么想法吗

谢谢

- (void)setupPage
{
scrollView.delegate = self;    
[self.scrollView setBackgroundColor:[UIColor grayColor]];
[scrollView setCanCancelContentTouches:NO];

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;

int initialXPos = 0;
int initialYPos = 10;

int arraycounter=0;
CGFloat cx = 0;
int aWidth = 0;

for (int j=0; j<3; j++) 
{
    if (j == 0)
    {
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(initialXPos, initialYPos, 320, 250)];
        myView.backgroundColor = [UIColor redColor]; 

        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(initialXPos, 0, 320, 250) style:UITableViewStylePlain];
        self.tableView = tableView;

        tableView.dataSource = self;
        tableView.tag = 1;        
        [tableView reloadData];

        [myView addSubview:tableView];
        [scrollView addSubview:myView];
    }

    if (j == 1)
    {
        UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(initialXPos, initialYPos, 320, 250)];
        myView1.backgroundColor = [UIColor redColor]; 

        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(initialXPos, 0, 320, 600) style:UITableViewStylePlain];
        self.tableView = tableView;

        tableView.dataSource = self;
        tableView.tag = 2;        
        [tableView reloadData];

        [myView1 addSubview:tableView];
        [scrollView addSubview:myView1];
    }

    initialXPos = initialXPos + 320 ;
    arraycounter=arraycounter+1;
    aWidth = aWidth + 320;
    cx += scrollView.frame.size.width;
}
cx = aWidth;
self.pageControl.numberOfPages = arraycounter;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
-(无效)设置页面
{
scrollView.delegate=self;
[self.scrollView setBackgroundColor:[UIColor grayColor]];
[滚动视图设置CancelContentTouches:否];
scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds=是;
scrollView.scrollEnabled=是;
scrollView.PaginEnabled=是;
int initialXPos=0;
int initialYPos=10;
int-arraycounter=0;
cgcx=0;
int aWidth=0;

对于(int j=0;j你应该这样做

[滚动视图添加子视图:tableView1]

[滚动视图添加子视图:tableView2]

[myView addSubview:scrollView]

表示在滚动视图中添加前两个表,然后在主视图中添加滚动视图
我用这种方法解决了这个问题。

在j==1的情况下设置tableView的框架时,请确保使用相对于myView1的坐标。在创建scrollview时,似乎出现了问题。我在viewdidload上运行了上述例程,但当我将其移动到-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)时nibBundleOrNil它完全失败。忽略最后一条评论!我已经解决了这个问题。我已经删除了UIView,并将UITableView直接添加到scrollview中,它工作得非常好。