objective-c - UIScrollview 中的多个 Tableview

标签 objective-c ios uitableview uiscrollview

我根据数组计数将多个 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/

相关文章:

ios - 即使里面没有数据,如何设置 UITableViewCell 的自定义背景颜色

ios - 数组未通过委托(delegate)从 TableView 传递到 View Controller

ios - 如何在没有混合图层的情况下获得具有渐变的图像?

java - 解析 withinMiles 首先返回最近的

ios - AFNetworking 2.0 + UIProgressView 如何正确显示上传 session 的进度

ios - CocoaPods:在 pod 中使用静态库

ios - 如何使用 Object-C 设置 BSImagePickerViewController

iphone - 无法摆脱顶部导航栏

ios - 向具有多个自定义注释 View 的 map View 添加集群注释

ios - 清除搜索结果后,Swift Tableview 搜索结果单元格选择复选标记不保存