gpt4 book ai didi

objective-c - UIScrollview 中的多个 TableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:19 25 4
gpt4 key购买 nike

我正在根据数组计数将多个 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)];

它在模拟器中显示正常。我需要用数组填充 TableView 。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/

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