我正在尝试将多个食物菜单数组加载到一个TableViewController中。主菜单显示2个按钮,1:早餐菜单2:面包店菜单。每次我按下它们时,模拟器都会崩溃并显示SIGABRT错误。经过一个不眠之夜的自我尝试后,我正在联系你看看它......我用segues检查了所有的连接和按钮,然后检查出来..
我的单元格有3个标签和一个图像,每个标签都有自己的数组。我将按钮分开以在不同的内容模式中进行选择。内容模式1:早餐&内容模式2:面包店..
对不起,伙计们真的想学习如何做到这一点!
按钮菜单控制器:
import UIKit
class foodMenuController:UIViewController {
@IBOutlet weak var mainMenuButton: UIButton!
var contentMode = 0
//Breakfast mode
@IBAction func breakfastButton(sender: AnyObject) {
contentMode == 1
}
//Bakery mode
@IBAction func bakeryButton(sender: AnyObject) {
contentMode = 2
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Hide navigation bar
self.navigationController?.navigationBarHidden = true
func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
TABLEVIEWCONTROLLER
import UIKit
//Return to Food Menu
@IBOutlet weak var returnButton: UIBarButtonItem!
// BREAKFAST MENU
let breakfastMenu = ["SAVORY CROISSANT", "HEARTY CROISSANT", "LE PETIT DÉJEUNER", "BREAKFAST in AMERICA", "SCRAMBLED EGGS", "FRESH FRUIT BOWL", "OMELETTE", "OATMEAL CUP", "PARIS SANDWICH", "EGGS BENEDICT", "CHEESE BAGEL", "EGGS BALTIQUE", "TOASTED BAGEL", "TOAST"]
// Breakfast menu images
let breakfastMenuImages = ["SAVORY CROISSANT.bmp", "HEARTY CROISSANT.bmp", "LE PETIT DÉJEUNER.bmp", "BREAKFAST in AMERICA.bmp", "SCRAMBLED EGGS.bmp", "FRESH FRUIT BOWL.bmp", "OMELETTE.bmp", "OATMEAL CUP.bmp", "PARIS SANDWICH.bmp", "EGGS BENEDICT.bmp", "CHEESE BAGEL", "EGGS BALTIQUE.bmp", "TOASTED BAGEL.bmp", "TOAST.bmp"]
// Meals definition
let breakfastMealDefined = ["Stuffed with eggs, ham and swiss cheese", "Stuffed with eggs, bacon and swiss cheese", "French baguette with butter, jam", "Two eggs, bacon, sausage, roasted potatoes","Scrambled eggs & smoked salmon","Seasonal fruits with a honey yogurt dressing.","Three fluffy eggs. Add 70 cents per ingredient choice:","Warm oatmeal with milk & extra's","French baguette with ham, cheese & butter","Two poached eggs on an English muffin","Toasted bagel with cream cheese and smoked salmon","Two poached eggs on an English muffin","Toasted bagel with cream cheese & jam","Choose from white, wheat or rye with jelly"]
// Meal price
var breakfastPrice = ["$8,25","$8,25","$7,25","$9,95","$9,25","$5,95","$7,95","$3,95","$9,25","$9,50","$10,25","$11,95","$3,75","$2,75"]
//BAKERY MENU
let bakeryMenu = ["SAVORY CROISSANT", "HEARTY CROISSANT", "LE PETIT DÉJEUNER"]
//Bakery menu images
let bakeryMenuImages = ["SAVORY CROISSANT.bmp", "HEARTY CROISSANT.bmp", "LE PETIT DÉJEUNER.bmp"]
// Bakery definition
let bakeryMealDefined = ["Stuffed with eggs, ham and swiss cheese", "Stuffed with eggs, bacon and swiss cheese", "French baguette with butter, jam"]
// Meal price
var bakeryPrice = ["$8,25","$8,25","$7,25"]
// --> VIEW DID LOAD <-- //
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if foodMenuController().contentMode == 1 {
return breakfastMenu.count
}
if foodMenuController().contentMode == 2 {
return bakeryMenu.count
} else{
return foodMenuController().contentMode; 1
}
}
// CONTENT MODE 1
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as! firstTableViewSettings
if foodMenuController().contentMode == 1 {
// Cell Configuration (image, text labels, round images)
cell.menuImageOption.image = UIImage (named: breakfastMenu[indexPath.row])
cell.mealPrice.text = breakfastPrice[indexPath.row]
cell.mealDescription.text = breakfastMealDefined[indexPath.row]
cell.menuChoicelabel.text = breakfastMenu[indexPath.row]
cell.menuImageOption.layer.cornerRadius = 30.0
cell.menuImageOption.clipsToBounds = true
}
//CONTENT MODE 2
if foodMenuController().contentMode == 2 {
// Cell Configuration (image, text labels, round images)
cell.menuImageOption.image = UIImage (named: bakeryMenu[indexPath.row])
cell.mealPrice.text = bakeryPrice[indexPath.row]
cell.mealDescription.text = bakeryMealDefined[indexPath.row]
cell.menuChoicelabel.text = bakeryMenu[indexPath.row]
cell.menuImageOption.layer.cornerRadius = 30.0
cell.menuImageOption.clipsToBounds = true
}
return cell
}
}
TABLEVIEWCELL
import UIKit
class firstTableViewSettings:UITableViewCell {
// Outlets for prototype cell configuration
@IBOutlet weak var menuImageOption: UIImageView!
@IBOutlet weak var menuChoicelabel: UILabel!
@IBOutlet weak var mealDescription: UILabel!
@IBOutlet weak var mealPrice: 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
}
}
答案 0 :(得分:0)
通常,将数据向前传递到下一个视图控制器比从前一个视图控制器获取数据更容易。因此,将contentMode
移至BreakfastFoodControllerTableViewController
(这是有道理的;它决定了TVC的&#34;模式&#34;)。
var contentMode = 0
然后将foodMenuController().contentMode
的每个引用修改为contentMode
。
接下来,修改FoodMenuController
以在segue到BreakfastFoodControllerTableViewController
之前设置正确的值。执行此操作的位置在FooodMenuController
prepareForSegue
方法中:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let breakfastVC = segue.destinationViewController as! BreakfastFoodControllerTableViewController
if segue.identifier == "showBreakfast" {
breakfastVC.contentMode = 1
} else if segue.identifier == "showBakery" {
breakfastVC.contentMode = 2
}
}
您需要确保每个segue在故事板中都有正确的标识符:
不再需要IBAction
方法breakfastButton
和bakeryButton
。