gpt4 book ai didi

iphone - 如何使用PageControl在UIScrollView中添加UITableView

转载 作者:行者123 更新时间:2023-12-03 20:52:52 25 4
gpt4 key购买 nike

在我的 ViewController 中,我有两个 uitableview。其中一个是静态的,有一节两行,另一个有一节四行。我想用 UIPageControl 做一个 UIScrollview ,并且在每个页面中,我想添加第二个包含四行的 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。

感谢您的帮助。

最佳答案

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

问题在于连接数据源。您需要将每个 TableView 的数据源属性绑定(bind)到循环内的 View Controller 。然后根据循环索引为循环中的每个表指定不同的 .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...
}
}

关于iphone - 如何使用PageControl在UIScrollView中添加UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8892106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com