【发布时间】:2015-02-05 14:32:31
【问题描述】:
所以我有一个名为FoodListTable 类型为UITableViewController 的类
我还有一个变量可以查找数组中元素的总和:
var calorieTotal: Float {
return calorieNumberArray.reduce(0 as Float) { $0 + Float($1) }
}
我做了一些测试,这个变量计算正确。但是,当我尝试在另一个名为 Menu 的类中使用它时,它的计算结果为 0.0。我试过这样做:
@IBAction func calculateButtonTappedMenu(sender: AnyObject) {
let foodListTableAccess = FoodListTable()
let dividend = foodListTableAccess.calorieTotal
但是当我这样做时,var calorieTotal 仍然计算为 0.0。我怎样才能解决这个问题?我对编程很陌生,所以请提供我需要修复它的所有代码。此外,如果需要,这里是两个类中的所有代码:
class FoodListTable: UITableViewController {
var calorieNumberArray = [0,0,0,0,0,0,0,0,0]
var foods = [Food]()
override func viewDidLoad() {
super.viewDidLoad()
self.foods = [Food(Name: "Small French Fries: 197 Cal."),Food(Name: "Cheeseburger: 359 Cal., One Patty"),Food(Name: "Cheese Pizza: 351 Cal., One Slice"),Food(Name: "Fried Chicken Breast: 320 Cal."),Food(Name: "Large Taco: 571 Cal."),Food(Name: "Hotdog: 315 Cal., With Ketchup"),Food(Name: "Tuna Sandwich: 287 Cal."),Food(Name: "1 Cup Vanilla Ice Cream: 290 Cal."),Food(Name: "1 1/2 Cup Vegetable Salad: 30 Cal.")]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.foods.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var food : Food
food = foods[indexPath.row]
cell.textLabel.text = food.Name
tableView.deselectRowAtIndexPath(indexPath, animated: true)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .None {
if indexPath.row == 0 {
calorieNumberArray[0] = 197
}
if indexPath.row == 1 {
calorieNumberArray[1] = 359
}
if indexPath.row == 2 {
calorieNumberArray[2] = 351
}
if indexPath.row == 3 {
calorieNumberArray[3] = 320
}
if indexPath.row == 4 {
calorieNumberArray[4] = 571
}
if indexPath.row == 5 {
calorieNumberArray[5] = 315
}
if indexPath.row == 6 {
calorieNumberArray[6] = 287
}
if indexPath.row == 7 {
calorieNumberArray[7] = 290
}
if indexPath.row == 8 {
calorieNumberArray[8] = 30
}
cell.accessoryType = .Checkmark
} else {
if indexPath.row == 0 {
calorieNumberArray[0] = 0
}
if indexPath.row == 1 {
calorieNumberArray[1] = 0
}
if indexPath.row == 2 {
calorieNumberArray[2] = 0
}
if indexPath.row == 3 {
calorieNumberArray[3] = 0
}
if indexPath.row == 4 {
calorieNumberArray[4] = 0
}
if indexPath.row == 5 {
calorieNumberArray[5] = 0
}
if indexPath.row == 6 {
calorieNumberArray[6] = 0
}
if indexPath.row == 7 {
calorieNumberArray[7] = 0
}
if indexPath.row == 8 {
calorieNumberArray[8] = 0
}
cell.accessoryType = .None
}
}
println(calorieNumberArray)
println(calorieTotal)
}
var calorieTotal: Float {
return calorieNumberArray.reduce(0 as Float) { $0 + Float($1) }
}
}
class Menu: Calculator {
@IBOutlet weak var yourWeightTextFieldMenu: UITextField!
@IBOutlet weak var exerciseListPickerViewMenu: UIPickerView!
@IBOutlet weak var calculateButtonMenu: UIButton!
@IBOutlet weak var calculatorContainerMenu: UIView!
@IBOutlet weak var answer1LabelMenu: UILabel!
override func viewDidLoad() {
calculatorContainerMenu.hidden = false
answer1LabelMenu.hidden = true
yourWeightTextFieldMenu.delegate = self
exerciseListPickerViewMenu.delegate = self
exerciseListPickerViewMenu.dataSource = self
calculateButtonMenu.enabled = false
yourWeightTextFieldMenu.addTarget(self, action:"yourWeightEditingChanged:", forControlEvents:.EditingChanged);
}
override func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
if textField == yourWeightTextFieldMenu {
yourWeightFilled = text.toInt() != nil
}
return true
}
override func textFieldShouldClear(textField: UITextField) -> Bool {
calculateButtonMenu.enabled = false
return true
}
@IBAction func yourWeightEditingDidEndMenu(sender: AnyObject) {
yourWeightTextFieldMenu.resignFirstResponder()
self.yourWeight = (self.yourWeightTextFieldMenu.text as NSString).floatValue
}
override func validateCalculateButton() {
self.calculateButtonMenu.enabled = (self.yourWeightFilled)
}
@IBAction func yourWeightEditingChangedMenu(sender: AnyObject) {
self.validateCalculateButton()
}
@IBAction func calculateButtonTappedMenu(sender: AnyObject) {
calculatorContainerMenu.hidden = true
answer1LabelMenu.hidden = false
let foodListTableAccess = FoodListTable()
let divisor = (yourWeight * exerciseCurrentValue)
let dividend = foodListTableAccess.calorieTotal
let result = (round(dividend / divisor))
answer1LabelMenu.text = "It will take you about \(result) minutes to burn off those menu choices by performing that exercise."
println("divisor: \(divisor), dividend: \(dividend), result: \(result)")
}
}
谢谢!
【问题讨论】:
-
是的...问题是您正在创建类的新实例,而不是获取当前实例。你在使用 UINavigationController 吗?
-
"所以请提供我需要修复它的所有代码。" – 不。
-
这个问题似乎是题外话,因为它是一个代码请求。
-
@TheParamagneticCroissant 老实说,该解决方案似乎需要大约 3 行代码。没那么严重。
-
OP,你是如何从
FoodListTable过渡到Menu的?
标签: ios class variables swift reduce