我试图做的是使用Delegate通过CalorieFooter单元中的按钮将数据传递到另一个视图控制器。
我无法成功通过CalorieFooter单元格中的infoButton传递数据以显示卡路里,卡路里和卡路里中的卡路里,卡路里和蛋白质。
我目前在CalorieBreakdownController的所有标签中都得到了“ 0”(如图像的最右边所示)。
我认为这个问题可能是因为我的calculationDelegate在CalorieFooter细胞来完成。而我得到小计的方法是将这些单元分成几部分。我对如何从页脚将数据传递到CalorieBreakdownController以获得标签中的“小计”感到困惑。
我如何能够从卡路里卡路里单元格传递CalorieBreakdownController的“ subTotal”数据(如下图所示)
import UIKit
class CalorieViewController: UIViewController {
var selectedFood: FoodList!
var additionalCalories: DailyCalories! // delegate code for footer
var calorieItems: [DailyCalories] = []
var groupedCalorieItems: [String: [DailyCalories]] = [:]
var dateSectionTitle: [DailyCalories] = []
@IBOutlet weak var calorieTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
groupedFoodItems = Dictionary(grouping: calorieItems, by: {$0.foodList.day})
dateSectionTitle = groupedCalorieItems.map{$0.key}.sorted()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CalorieTotalController { // delegate code for footer
vc.additionalCalories = self.additionalCalories
}
}
}
extension CalorieViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return dateSectionTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let date = dateSectionTitle[section]
return groupedCalorieItems[date]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let calorieCell = tableView.dequeueReusableCell(withIdentifier: "CalorieCell") as! CalorieCell
let date = dateSectionTitle[indexPath.section]
let calorieItemsToDisplay = groupedCalorieItems[date]![indexPath.row]
calorieCell.configure(withCartItems: calorieItemsToDisplay.foodList)
return calorieCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let calorieHeader = tableView.dequeueReusableCell(withIdentifier: "CalorieHeader") as! CalorieHeader
let headerTitle = dateSectionTitle[section]
calorieHeader.dateLbl.text = "Date: \(headerTitle)"
return calorieHeader
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let calorieFooter = tableView.dequeueReusableCell(withIdentifier: "CalorieFooter") as! CalorieFooter
let date = dateSectionTitle[section]
let arrAllItems = groupedCalorieItems[dispensary]!
var subtotal: Float = 0
for item in arrAllItems {
if item.foodList.selectedOption == 1 {
subtotal = subtotal + (Float(item.foodList.calorie1) * Float(item.foodList.count))
} else if item.foodList.selectedOption == 2 {
subtotal = subtotal + (Float(item.foodList.calorie2) * Float(item.foodList.count))
} else if item.foodList.selectedOption == 3 {
subtotal = subtotal + (Float(item.foodList.calorie3) * Float(item.foodList.count))
}
}
calorieFooter.cartTotal.text = "\(subtotal)"
calorieFooter.calculationDelegate = self // delegate code for footer
calorieFooter.additionalCalories! = ??? // can't get the right code set to allow the data to pass
calorieFooter.calculations! = ??? // can't get the right code set to allow the data to pass
return calorieFooter
}
}
extension CalorieViewController: CalculationDelegate { // delegate code for footer
func onTouchInfoButton(from cell: CalorieFooter) {
self.additionalCalories = cell.additionalCalories
self.performSegue(withIdentifier: "CalculateDailyCalorieCell", sender: self)
}
}
import UIKit
protocol CalculationDelegate: class { // delegate code for footer
func onTouchInfoButton(from cell: CalorieFooter)
}
class CalorieFooter: UITableViewCell {
weak var calculationDelegate: CalculationDelegate? // delegate code for footer
var additionalCalories: DailyCalories! // delegate code for footer
var calculations: [DailyCalories] = []
@IBOutlet weak var calorieTotal: UILabel!
@IBOutlet weak var additionalFeesBtn: UIButton!
@IBAction func overallTotalBtn(_ sender: Any) {
self.additionalFeesDelegate?.onTouchInfoButton(from: self) // delegate code for footer
}
}
class CalorieTotalController: UIViewController {
var additionalCalories: DailyCalories! // delegate code for footer
var calculations: [DailyCalories] = [] // delegate code for footer
@IBOutlet weak var calorieSubtotal: UILabel!
@IBOutlet weak var fatTotal: UILabel!
@IBOutlet weak var carbTotal: UILabel!
@IBOutlet weak var proteinTotal: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// calculations done for when data is passed through Delegate
var subtotal: Float = 0
for item in calculations {
if item.foodList.selectedOption == 1 {
subtotal = subtotal + (Float(item.foodList.calorie1) * Float(item.foodList.count))
} else if item.foodList.selectedOption == 2 {
subtotal = subtotal + (Float(item.productList.calorie2) * Float(item.foodList.count))
} else if item.foodList.selectedOption == 3 {
subtotal = subtotal + (Float(item.foodList.calorie3) * Float(item.foodList.count))
}
}
let protein = Float(subtotal * 0.25)
let carbs = Float(subtotal * 0.25)
let fat = Float(subtotal * 0.5)
calorieSubtotal.text = String(subtotal!)
proteinTotal.text = String(protein!)
fatTotal.text = String(fat!)
carbTotal.text = String(carbs!)
}
@IBAction func closeButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
print("Calories BreakDown")
}
}
使用委托可能是一个好主意。我只是编写了一些代码(未经测试!)来告诉您它如何工作:
扩展委托类:
protocol CalculationDelegate: class {
func onTouchInfoButton(from cell: CalorieFooter)
func getAdditionalCalories() -> Int
func getCalculations() -> Int // or whatever type this object should be
}
确保您的视图控制器遵循以下协议:
extension CalorieViewController: CalculationDelegate {
func onTouchInfoButton(from cell: CalorieFooter) { ... }
func getAdditionalCalories() -> Int {
return self.calories
}
func getCalculations() -> Int {
return self.calculations
}
}
向CalorieViewController
其中添加存储当前状态的局部变量(卡路里/计算量)
class CalorieViewController: UIViewController {
private var calories: Int = 0
private var calculations: Int = 0
... other code that is already in the UIViewController
}
确保在某个地方初始化这些变量!就像是:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
... same as before...
self.calories = subtotal // I guess?
self.calculations = calculations // put this somewhere where calculations is initialized
}
现在,数据应该在中可用CalorieFooter
。我在CalculationDelegate为时添加了默认值0 nil
:
let calories = calculationDelegate?.getAdditionalCalories() ?? 0
let calculations = calculationDelegate?.getCalculations() ?? 0
祝好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句