Kevin
Kevin

Reputation: 39

Why is my tableViewController not loading any data?

Im creating an app where different buttons in a ViewController load different menu's into the tableViewController. The buttons are linked by a prepare for segue and the menu's (arrays) are linked by a contentMode. 1: breakfast menu & 2: lunch menu. I had allot of help from someone setting this up but now the table is not loading any data... The cell has 3 labels which display an item, info and price. It changes value within the code when a contentMode is selected. Does anyone see the problem in my code? thanks a lot!

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


    }
}

Upvotes: 0

Views: 122

Answers (3)

Mathews
Mathews

Reputation: 743

I have made slight modifications in your project. 1. make the UINavigationController the InitialViewController 2. make the FoodMenuController the root of UINavigationController StoryBoard

Now modify your 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
    }
}

Also you can make UINavigationBar visible in FoodTableViewController

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

PS: It is always better not to add segue directly to a UIButton. Alternatively you can add it from the yellow button on top of your FoodMenuController and specify the segue to be fired in UIButtonAction using performSegueWithIdentifier

Upvotes: 0

Amrit Sidhu
Amrit Sidhu

Reputation: 1950

I can no where see you setting the datasource and delegate of the tableView, please cross check these are both setup.

Upvotes: 0

lostInTransit
lostInTransit

Reputation: 71047

There isn't anything apparently wrong with the snippet you shared. You can check what is returned in the tableView:numberOfRowsInSection: method and see if it is returning a value > 0

Also, this is a given but we've all done it at some point of time - check to make sure the tableview delegate and datasource are set to your viewcontroller.

Upvotes: 1

Related Questions