如何使 NSArray 成为 cell.textLabel.text 可接受的值?

How to make NSArray an acceptable value for cell.textLabel.text?

简单的食谱应用程序

在我的 recipeBook 应用程序中,我有两个 UITableViewController。第一个 UITableViewController 包含一个带有菜谱名称列表的 UITableView。如果您 select 一个单元格,您将转到第二个 UITableViewController。第二个 UITableViewController 包含一个带有成分列表的 UITableView。

在我的应用程序中,我使用 RecipeObject class,它包含两个属性(名称(类型:NSString)和成分(类型:NSArray))。recipeObject 对象都在 RecipeData class 中声明,像这样:

RecipeObject *recipe1 = [[RecipeObject alloc]init];
recipe1.name = @"Fresh Coconut Cake";
recipe1.ingredients = [NSArray arrayWithObjects:@"Coconut cups", @"Milk", @"Baking powder", @"Butter", @"Sugar", @"Eggs", nil];

在第一个 UITableViewController 中,我设法打印出每个单元格的配方名称。见以下代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"recipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

TostiObject *recipes = [self.recipes objectAtIndex:indexPath.row];
cell.textLabel.text = recipes.name;

return cell;

我在名为 self.recipes 的第一个 UITableViewController 中声明了一个 NSArray 属性 来连接 RecipeData。我还在第二个 UITableViewController 中做了以下操作。

self.recipes = [RecipeData allRecipes];

问题出现在第二个UITableViewController:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ingredientCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];

cell.textLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];

return cell;

因为cell.textLabel.text只接受一个NSString值而recipe.ingredients是一个NSArray。我想将成分数据(一个 NSArray)连接到第二个 UITableViewController 中的 cell.textLabel.text。

也许有人可以帮我解决这个问题。非常感谢所有帮助!

- 其他信息 -

prepareForSegue 方法(第一个 UITableViewController):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ingredients"]) {
        SecondTableViewController *IngredientsTVC = (SecondTableViewController *)segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
        IngredientsTVC.recipeContainer = recipe.ingredients;

你应该只将一个食谱 object 传递给你的第二个 table 视图控制器(所以我不明白你为什么在 cellForRowAtIndexPath 中使用 RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row] ).然后,您可以使用 recipe.ingredients 作为用于填充 table 的数组。您应该仍然可以在 cellFroRowAtIndexPath,

中使用这一行
cell.textLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];

您还应该将 recipe.ingredients.count 传递给 numberOfRowsInSection

编辑后:

有几件事不清楚您在做什么。您似乎希望 SecondTableViewController 显示来自第一个控制器的单个选定配方的成分列表。因此,首先,根本不需要在 SecondTableViewController 中添加 self.recipes = [RecipeData allRecipes] 这一行;该控制器只需要知道通过 prepareForSegue 传入的一个配方。我假设 recipeContainer 是 SecondTableViewController 的数组 属性,它是成分数组;这就是您填充 table 所需的内容(顺便说一句,没有必要在 prepareForSegue 中传递它,因为您可以从传递的食谱 object 中获取它)。所以,你应该这样做的方法是,首先,删除你在 prepareForSegue 中显示的最后一行;你在那里不需要它。

在SecondTableViewController.h中

@property (strong,nonatomic) RecipeObject *recipe;

在SecondTableViewController.m中

@interface SecondTableController ()
@property (strong,nonatomic) NSArray *ingredients;
@end

@implementation SecondTableController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.ingredients = self.recipe.ingredients;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.ingredients.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.ingredients[indexPath.row];
    return cell;
}

如果需要,您可以使用 self.title = recipe.name

在 vi​​ewDidLoad 中设置 SecondViewController 的标题