我正在尝试将多个食物菜单数组加载到一个 TableViewController 中。主菜单显示 2 个按钮,1:早餐菜单 2:面包菜单。每次我按下它们时,模拟器都会崩溃并显示 SIGABRT 错误。在自己尝试了一个不眠之夜之后,我联系你来看看它......我用 segues 检查了所有的连接和按钮,一切都检查好了......
我的单元格有 3 个标签和一个图像,每个都有自己的数组。我划分了按钮以选择不同的内容模式。内容模式 1:早餐 & 内容模式 2:面包店..
对不起大家真的很想学习如何做到这一点!
按钮菜单 Controller :
import UIKit
类 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.
}
}
表格 View Controller
import UIKit
类 breakfastFoodControllerTableViewController: UITableViewController {
//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
}
最佳答案
一般来说,将数据向前传递到下一个 View Controller 比从前一个 View Controller 获取数据更容易。因此,将 contentMode
移动到 BreakfastFoodControllerTableViewController
(这是有道理的;它决定了 TVC 的“模式”)。
var contentMode = 0
然后将 foodMenuController().contentMode
的每个引用修改为 contentMode
。
接下来,修改 FoodMenuController
以在 BreakfastFoodControllerTableViewController
的 segue 之前设置正确的值。执行此操作的位置是在 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 在您的 Storyboard中都有正确的标识符:
不再需要 IBAction
方法 breakfastButton
和 bakeryButton
。
关于arrays - 错误 SIGABRT - 无法在我的 TableViewController 中加载多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36036160/