我根据数组计数将多个 TableView 添加到 ScrollView 中......代码在这里
int currentX = 3;
for(int i=0;i<[restNameArray count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(currentX, 0, 150, 35)];
button.tag = i+1;
//[button setUserInteractionEnabled:NO];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"ohris_applytics_ipad_middle_oc.png"] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:15.0f];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%@", [restNameArray objectAtIndex:i]] forState:UIControlStateNormal];
restaurantTable = [[UITableView alloc] initWithFrame:CGRectMake(currentX, 35, 150, 310) style:UITableViewStylePlain];
[restaurantTable setTag:i+1];
[restaurantTable setBackgroundColor:[UIColor whiteColor]];
restaurantTable.delegate = self;
restaurantTable.dataSource = self;
[scrollView addSubview:button];
[scrollView addSubview:restaurantTable];
NSLog(@"tag %d",restaurantTable.tag);
currentX = restaurantTable.frame.size.width+currentX+3;
[tagArray addObject:[NSString stringWithFormat:@"%d", restaurantTable.tag]];
}
NSLog(@"aaaa %@", tagArray);
[scrollView setContentSize:CGSizeMake(currentX, 349)];
它在模拟器中显示良好。我需要用数组填充表格 View 。 cellforrowatindexpath 处的代码是
if(tableView == restaurantTable){
for (int i =0;i<[tagArray count];i++)
{
table1Array = [[[restSalesArray objectAtIndex:i]componentsSeparatedByString:@","]mutableCopy];
NSLog(@"Sales array :%d %@", i, table1Array);
cell.textLabel.text = [formatter stringFromNumber:[NSNumber numberWithInteger:[[table1Array objectAtIndex:indexPath.row]intValue]]];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
但问题是,它只在最后一个表格 View 中填充..剩下的都是空的...请帮助我......
最佳答案
你正在尝试做的事情没问题。但是您正在覆盖 for 循环中的 restaurantTable
变量。所以变量就是最后一个 TableView 。您需要将它们添加到数组或单独的变量中,以便能够与 cellForRowAtIndexPath
方法中的 if 进行比较。
_allTableViews 在您的头文件中定义
你的for循环
_allTableViews = [NSMutableArray array];
for (.....) {
.
.
.
UITableView *tableView = [[UITableView alloc] initWith....];
.
.
.
[_allTableViews addObject tableView];
}
cellForRowAtIndePath
NSInteger tableIndex = [_allTableViews indexOfObject:tableView];
// Now you have the index of table view
switch (tableIndex) {
case 0:
// first table
break;
case 1:
// second table
break;
default:
break;
}
关于objective-c - UIScrollview 中的多个 Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056287/