2017-04-01 10 views
0

テーブルの食事リストが表示されているアプリを作成しています。メニューをクリックすると、詳細が表示されます。Xcode TableViewCellのViewControllerへのプッシュ

私はそれを実行すると、それはテーブルの上に食事を表示しますが、私は、それだけで食事テンプレートを与える食事に

This is a picture of the storyboard をクリックすると、ここで私はアイテムを表示するために、私のTableViewControllerのために使っていたコードがありますコードで

private func loadSampleMeals() { 

     let photo1 = UIImage(named: "Sprite") 
     let photo2 = UIImage(named: "HotCheetos") 
     let photo3 = UIImage(named: "Nachos") 

     guard let meal1 = Meal(name: "Sprite", price: "$1.50", photo: photo1, rating: 0, calories: "Calories: 129", description: "Description: A refreshing lemon-lime soda") else { 
      fatalError("Unable to instantiate meal1") 
     } 

     guard let meal2 = Meal(name: "Hot Cheetos", price: "$1.50", photo: photo2, rating: 0, calories: "Calories: 160", description: "Description: A spicy version of original cheetos") else { 
      fatalError("Unable to instantiate meal2") 
     } 

     guard let meal3 = Meal(name: "Nachos", price: "$1.50", photo: photo3, rating: 0, calories: "Calories: 436", description: "Description: Tortilla chips with a side of smooth nacho cheese") else { 
      fatalError("Unable to instantiate meal2") 
     } 

     meals += [meal1, meal2, meal3] 
    } 


     override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     super.prepare(for: segue, sender: sender) 

     switch(segue.identifier ?? "") { 
      case "ShowDetail": 
      guard let mealDetailViewController = segue.destination as? MealViewController else { 
        fatalError("Unexpected destination: \(segue.destination)") 
      } 

      guard let selectedMealCell = sender as? MealTableViewCell else { 
       fatalError("Unexpected sender: \(sender)") 
      } 

      guard let indexPath = tableView.indexPath(for: selectedMealCell) else { 
       fatalError("The selected cell is not being displayed by the table") 
      } 

      let selectedMeal = meals[indexPath.row] 
      mealDetailViewController.meal = selectedMeal 

     default: 
      fatalError("Unexpected Segue Identifier; \(segue.identifier)") 
     } 
    } 

、ガード文は、それがのViewControllerの項目のデータを表示させる必要がありますが、私はあなたが01で食事オブジェクトにアクセスすることができます下

+0

詳細テーブルコントローラーを変更している間にデータを渡しましたか? –

+0

あなたが行っていることを私たちに知らせるtableViewメソッドでいくつかのコードを表示することができます –

+0

トップナビゲーションコントローラを削除してください...アプリ内にはナビゲーションコントローラが1つだけ必要です... –

答えて

0

でデフォルトのエラーを取得しますたとえば、セルを生成する食事の配列があり、1つのセクションしか表示されません。

var meals: [Meal] 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedMeal = meals[indexPath.row] 
    let mealDetailsVC = MealDetailsViewController() 
    mealDetailsVC.meal = selectedMeal 

    navigationController?.pushViewController(mealDetailsVC, animated: true) 
} 
関連する問題