【问题标题】:How to make NSArray an acceptable value for cell.textLabel.text?如何使 NSArray 成为 cell.textLabel.text 的可接受值?
【发布时间】:2015-03-29 00:02:27
【问题描述】:

简单的食谱应用程序

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

在我的应用程序中,我使用了一个 RecipeObject 类,它包含两个属性(名称(类型:NSString)和成分(类型:NSArray)。recipeObject 对象都在 RecipeData 类中声明,如下所示:

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;

我在第一个 UITableViewController 中声明了一个名为 self.recipes 的 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;

【问题讨论】:

  • 标题问题的答案是,你不能;您只能将字符串传递给 text 属性,而不是数组。

标签: ios objective-c uitableview nsarray cell


【解决方案1】:

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

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

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

编辑后:

有几件事不清楚你在做什么。您似乎希望 SecondTableViewController 显示来自第一个控制器的单个选定配方的成分列表。所以,首先,根本不需要在 SecondTableViewController 中有self.recipes = [RecipeData allRecipes]这一行;该控制器只需要知道通过 prepareForSegue 传入的一个配方。我假设 recipeContainer 是 SecondTableViewController 的一个数组属性,它是成分数组;这是您填充表格所需的(顺便说一句,没有必要在 prepareForSegue 中传递它,因为您可以从传递的配方对象中获取它)。所以,你应该这样做,首先,删除你在 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;
}

如果你愿意,你可以在 viewDidLoad 中使用 self.title = recipe.name 设置 SecondViewController 的标题

【讨论】:

  • 您好,rdelmar,感谢您的回答。我只将一个配方对象传递给第二个 UITableViewController。这是因为我在不同的类中声明了配方信息。查看我的问题的编辑版本。
  • @Nielsapp,编辑您的问题以显示与第二个控制器中的表视图相关的所有代码。
  • 我用第二个表格视图的信息更新了问题。请参阅-项目信息第二个视图控制器中的文本-谢谢!
  • 感谢 rdelmar!效果很好:)祝您有美好的一天!
猜你喜欢
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多