视图控制器中的多个表视图

Multiple tableview in a view controller

我在视图控制器中有 2 个 table 视图。一个 table 视图显示成分列表,另一个显示方向列表。

我需要 TableView1 来显示列表而不截断列表。现在它只显示两个项目,其余的都被标签 "Directions".

隐藏了

我希望 table 视图将标签向下推以显示列表中的所有项目。如何在不滚动 table 视图的情况下一次显示所有 table 视图数据?

第二个 Table 视图遵循相同的故事。它显示了食谱说明列表。现在它只会显示 3-4 个项目。基本上与我在情节提要中的大小相关。我基本上希望尺寸是动态的。

问题出在 UI。 这是我尝试过的。

  1. 滚动启用 = 假
  2. 剪辑子视图 = 假
  3. 自动调整子视图大小 = False

此外,这是我在 table 视图

中填充列表的代码
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Method call");
    if (tableView == self.ingredientsTableView) {
        static NSString *cellIdentifier = @"Ingredients Cell";
        UITableViewCell *ingredientCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!ingredientCell) {
            ingredientCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        //display the ingredient
        ingredientCell.textLabel.text = [NSString stringWithFormat: @"%@", self.recipe.ingredients[ indexPath.row]];
        NSLog(@"Ingredient %@", ingredientCell.textLabel.text);

        //return ingredient
        return ingredientCell;

    } else if (tableView == self.directionsTableView) {
        static NSString *cellIdentifier = @"Directions Cell";
        UITableViewCell *directionCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!directionCell) {
            directionCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        //display the direction
        directionCell.textLabel.text = [NSString stringWithFormat: @"%@", self.recipe.directions[ indexPath.row]];
        NSLog(@"Directions %@", directionCell.textLabel.text);

        //return ingredient
        return directionCell;

    }

    return  nil;
}

如果您想一次显示所有数据而无需 table 滚动,那么为什么要使用 UITableView?查看您的问题,您似乎只 NSString 在 table 视图中显示。我建议你放弃 UITableView 想法并选择 UILabel 显示。

即使您的应用程序需要 UITableView,您也必须在加载时提供 contentSize / UITableView 的高度,具体取决于数组中负责填充的项目数UITableView.

例如。 contentSize/height 的 table 视图 =(数组中的项目数)*(行的高度);