Soy nuevo en stack overflow y en swift. Estoy haciendo una aplicación de recetas de cocina y una de las funciones es mostrar ciertas recetas que contengan el ingrediente seleccionado(ingredientDetailViewController).
Lo que quiero es que a su vez al seleccionar, de una descripción de la receta con la preparación. Ya tengo una vista que hace esto con un singleton(DetailViewController) y pensaba reutilizarla pero no consigo que funcione. El caso es que no consigo pasar los datos de arrayingrediente de una vista a otra, cuando hago un debug me aparece:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) y fatal error: Index out of range
Código de ingredientDetailViewController:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath=tableView.indexPathForSelectedRow!
let currentCell=tableView.cellForRow(at: indexPath)! as UITableViewCell
performSegue(withIdentifier: "idetailrecipe", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier=="idetailrecipe"){
if let indexPath = self.tableView.indexPathForSelectedRow{
let viewController=segue.destination as! DetailViewController
viewController.arrayingrediente[indexPath.row]=arrayingrediente[indexPath.row]
}
}
}
Código de DetailViewController:
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell") as! DetailTableViewCell
if arrayingrediente.isEmpty{
cell.titulo?.text=receta?.titulo
if let image=receta?.imagen{
cell.imagen?.image=UIImage.init(named:image)
}
cell.ingredientes?.text=receta?.ingrediente
cell.preparacion?.text=receta?.preparacion
}
else{
cell.titulo?.text=arrayingrediente[indexPath.row].titulo
cell.imagen?.image=UIImage.init(named:arrayingrediente[indexPath.row].imagen)
cell.ingredientes?.text=arrayingrediente[indexPath.row].ingrediente
cell.preparacion?.text=arrayingrediente[indexPath.row].preparacion
}
return cell
}