-1

Display details in second UITableViewController

My application is a simple Recipebook to understand UITableViewControllers. The application contains two UITableViewControllers. The first UITableViewController contains a UITableView with a list of the recipe names. If you select a cell you will segue to the second UITableViewController. The second UITableViewController contains a UITableView with a list of the ingredients.

The application contains the following classes:

  • RecipeTableViewController (first)
  • IngredientTableViewController (second)
  • RecipeObject
  • RecipeData

The RecipeObject Class contains two properties. One property of type NSString with the recipe name. The other property is of type NSArray with the ingredients. The RecipeObject objects are in the 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];

The RecipeData is called in the RecipeTableViewController to display the recipe names in the tableView.

Message from RecipeData class to RecipeTableViewController:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.recipes = [RecipeData allRecipes];}

Display names in the tableView:

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

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

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

    return cell;
}

How to add the recipe1.ingredients array to the IngredientsTableViewController?

Any help is highly appreciated!

Hima
  • 1,249
  • 1
  • 14
  • 18
Nielsapp
  • 302
  • 1
  • 11
  • That contains a lot of good information but not what I need. My question: how do I get the recipe1.ingredients array of the RecipeObject in the tableviewcontroller. – Nielsapp Mar 28 '15 at 22:03

1 Answers1

0

Perform a segue when a recipe is selected. In the prepare for segue-method take the selected recipe and pass the ingredients to the ingredientsTableViewController. Something quite similar to this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"to_Ingredients" sender:self];
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"to_Ingredients"]) {
        IngredientTableViewController *ingredientsTableViewController = (IngredientTableViewController *)[segue destinationController];

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
    ingredientsTableViewController.ingredients = recipe.ingredients;

    }

}

If you don't use a storyboard where you set this segue you just need the first method which the should looks like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

IngredientTableViewController *ingredientsTableViewController = [[IngredientTableViewController alloc]init];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
ingredientsTableViewController.ingredients = recipe.ingredients;
[self showDetailViewController:ingredientsTableViewController sender:self]
}
  • Hey Finn FahrenKrug, thanks a lot for your answer. When I implant your code I get the following error: "Incompatible pointer types assigning to 'NSSrting' from 'NSArray'". This error occurs when I pass recipe.ingredients in the cell.textLabel.text of the IngredientsTableViewController. It only accepts an NSString value. Maybe you know how to convert the ingredients array to an acceptable value for cell.textLabel.text of the IngredientsTableViewController. Man, thanks again. You're my hero. – Nielsapp Mar 28 '15 at 22:28
  • No problem. The error occures, because you have to pass only one ingredient to a cell and not all ingredients. Change the code-line to the following: cell.textLabel.text = [self.ingredients objectAtIndex:indexPath.row]; – Finn Fahrenkrug Mar 28 '15 at 22:44
  • I changed the code to your instructions, but it didn't worked. It's because I didn't declare the ingredients in (self) but in the class RecipeObject. Then the ingredients data is passed to an NSArray property in the IngredientsTableViewController. Maybe I need to create an RecipeObject in cellForRowAtIndexPath, something like: RecipeObject *recipes = [..]? I think you are very close in solving the problem. I hope you could help me with this final step. You're very much appreciated! – Nielsapp Mar 28 '15 at 23:36
  • Okay, i think we're close. You said, you are passing the ingredients data to an NSArray property? That's good. I was assuming that this property is called 'ingredients'. Change the line again: cell.textLabel.text = [self.'nameOfTheArrayProperty' objectAtIndex:indexPath.row]; – Finn Fahrenkrug Mar 28 '15 at 23:52