2017-04-01 70 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)") 
     } 
    } 

在代碼中,保護報表應使其顯示在視圖控制器的項目數據,但我得到了默認的錯誤在底部

+0

您是否通過數據,而改變tableview控制器到詳細控制器? –

+0

你可以用你的tableView方法顯示一些代碼,它會告訴我們你正在做什麼 –

+0

刪除頂部導航控制器...在應用程序中應該只有1個導航控制器... –

回答

0

您可以01訪問了一頓對象,例如,你有一個餐飲生成陣列只有一個部分

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) 
} 
相關問題