为什么我的tableViewController不加载任何数据?

我创build一个应用程序,在ViewController中的不同button加载到tableViewController不同的菜单。 这些button通过为segue做准备而链接,菜单(数组)通过contentMode链接。 1:早餐菜单和2:午餐菜单。 我已经从某人的帮助设置了这一点,但现在表没有加载任何数据…单元格有3个标签显示项目,信息和价格。 当selectcontentMode时,它将更改代码内的值。 有没有人看到我的代码中的问题? 非常感谢!

import UIKit class foodMenuController: UIViewController { override func viewDidLoad() { super.viewDidLoad() func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let foodMenuController = segue.destinationViewController as! foodTableViewController if segue.identifier == "showBreakfast" { foodMenuController.contentMode = 1 } else if segue.identifier == "showLunch" { foodMenuController.contentMode = 2 } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

 import UIKit class foodTableViewCell: UITableViewCell { @IBOutlet weak var foodItem: UILabel! @IBOutlet weak var foodDescription: UILabel! @IBOutlet weak var foodPrice: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } 

 import UIKit class foodTableViewController: UITableViewController { //Content Mode Selection in menu var contentMode = 0 // THIS SHOULD BE LOADED WHEN CONTENT MODE is "1" --> BREAKFAST let breakfastItems = ["Bread", "Coffee", "Nada"] let breakfastInfo = ["Good", "Nice", "Nothing"] let breakfastPrice = ["$1", "$100", "$12,40"] // THIS SHOULD BE LOADED WHEN CONTENT MODE IS "2" --> LUNCH let lunchItems = ["Not bread", "Not Coffee", "Something"] let lunchInfo = ["Not good", "Not nice", "Yes"] let lunchPrice = ["$1", "$100", "$12,40"] var foodItems: [String] = [] var foodInfo: [String] = [] var foodPrice: [String] = [] override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) switch (contentMode){ case 1: contentMode = 1 foodItems = breakfastItems foodInfo = breakfastInfo foodPrice = breakfastPrice case 2: contentMode = 2 foodItems = lunchItems foodInfo = lunchInfo foodPrice = lunchPrice default: break } tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return foodItems.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "Cell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! foodTableViewCell cell.foodItem.text = foodItems[indexPath.row] cell.foodDescription.text = foodInfo[indexPath.row] cell.foodPrice.text = foodPrice[indexPath.row] return cell } } 

我已经在你的项目中做了些微的修改。 1.使UINavigationController成为InitialViewController 2.使FoodMenuController成为UINavigationController的根 故事板

现在修改你的FoodMenuController

 @IBOutlet weak var bakeryButton: UIButton! @IBOutlet weak var breakfastButton: UIButton! override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBarHidden = true //hide navigationBar in first ViewController self.bakeryButton.addTarget(self, action: "bakeryButtonAction:", forControlEvents: .TouchUpInside) self.breakfastButton.addTarget(self, action: "breakfastButtonAction:", forControlEvents: .TouchUpInside) } func bakeryButtonAction(sender: UIButton) { performSegueWithIdentifier("showLunch", sender: self) } func breakfastButtonAction(sender: UIButton) { performSegueWithIdentifier("showBreakfast", sender: self) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let foodTableViewController: FoodTableViewController = segue.destinationViewController as! FoodTableViewController if segue.identifier == "showBreakfast" { foodTableViewController.contentMode = 1 } else if segue.identifier == "showLunch" { foodTableViewController.contentMode = 2 } } 

你也可以使UINavigationBarFoodTableViewController可见

  override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBarHidden = false } 

PS:最好不要直接添加segue到UIButton 。 或者,你可以从你的FoodMenuController顶部的黄色button添加它,并指定在UIButtonAction使用performSegueWithIdentifier

您分享的代码段没有任何明显的错误。 您可以检查在tableView:numberOfRowsInSection:方法返回什么,看看它是否返回值> 0

此外,这是一个给定的,但我们都已经做了一些时间 – 检查,以确保tableview委托和数据源设置为您的viewcontroller。

我不能在哪里看到你设置tableView的数据源和委托,请交叉检查这两个都设置。