Reputation: 39
After days of struggling I can't figure out how to fix this. I am making a simple restaurant app. The foodMenuController
contains 2 buttons which have to load different menu arrays into a tableViewController
(lunch,breakfast). To define what menu should be loaded I was told by someone to use a variable -> contentMode
.
In my TableViewController
i set the contentMode
to "0"
. In my foodMenuController
i used prepare for segue with a identifier to set the contentMode
to 1
(breakfast) and 2
(lunch).
Now the problem is that my table does not know when it is in contentMode 1 or 2 nor do I know where and how to write the code to indicate the table to load different arrays when in a certain contentMode. I hope someone can help me with the code i published below.
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 == "showBakery" {
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
var foodItems = ["Bread", "Coffee", "Nada"]
var foodInfo = ["Good", "Nice", "Nothing"]
var foodPrice = ["$1", "$100", "$12,40"]
// THIS SHOULD BE LOADED WHEN CONTENT MODE IS "2" --> LUNCH
var foodItems = ["Not bread", "Not Coffee", "Something"]
var foodInfo = ["Not good", "Not nice", "Yes"]
var foodPrice = ["$1", "$100", "$12,40"]
override func viewDidLoad() {
super.viewDidLoad()
}
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: 68
Reputation: 743
Change the array names as shown below.
// 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] = []
Now check the value of contentMode in ViewWillAppear
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
switch (contentMode){
case 1:
foodItems = breakfastItem
foodInfo = breakfastInfo
foodPrice = breakfastPrice
case 2:
foodItems = lunchItem
foodInfo = lunchInfo
foodPrice = lunchPrice
default:
break
}
tableView.reloadData()
}
This will copy the desired item, info ,price arrays into the foodItem, foodInfo and foodPrice arrays. The rest of your code will work fine and the UITableView
will be populated as desired.
Upvotes: 2