How to transfer a TableView from another TableView using segue?

0

I know how to transfer information from a TableViewController to a ViewController, but now I need to switch to another TableViewController using the function prepare(for segue: UIStoryboardSegue, sender: Any?) , plus I think you need to modify the value that returns tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int depending on how many cells each table has is it like that?

That is, each cell will direct you a different TableViewController and that other TableViewController will have a different number of cells than the rest.

I leave my code of how I pass information from a TableViewController to a ViewController so that you can help me with the changes that I have to make, thank you community. :)

var recipes : [Recipe] = []


override func viewDidLoad() {
    super.viewDidLoad()

    var recipe = Recipe(name: "Tortilla de patatas",
                        ingredients: ["Patatas", "Huevos", "Cebolla"])
    recipes.append(recipe)

    recipe = Recipe(name: "Pizza margarita",
                    ingredients: ["Harina", "Levadura", "Aceite", "Sal", "Salsa de Tomate", "Queso"])
    recipes.append(recipe)

}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.recipes.count
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showRecipeDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let selectedRecipe = self.recipes[indexPath.row]
            let destinationViewController = segue.destination as! DetailViewController
            destinationViewController.recipe = selectedRecipe
        }
    }
}
    
asked by Hober Mark 19.08.2017 в 05:31
source

1 answer

0

I think I understood that what you want is not to pass one of the recipes to a UIViewController but to a UITableViewController, I imagine that to show the ingredients as a new table (Even if it's not exactly that, it should help you)

You must create another UITableViewController like the one you show with some changes:

var recipe : Recipe?


override func viewDidLoad() {
    super.viewDidLoad()
    self.title = recipe.name //puedes usar esto para poner el nombre de la receta como título de la nueva lista
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.recipe.ingredients.count
}

This way, in the code that you already have, you just have to change the DetailViewController for this new UITableViewController

    
answered by 07.09.2017 / 12:58
source