gpt4 book ai didi

ios - 如何通过页脚单元格在表格 View 中使用委托(delegate)?

转载 作者:行者123 更新时间:2023-12-01 18:35:55 27 4
gpt4 key购买 nike

我试图做的是通过 CalorieFooter 单元格中的按钮将数据传递给另一个 View Controller ,使用委托(delegate)在单元格中传递数据。

我无法通过 中的 infoButton 成功传递数据卡路里页脚 单元格以在 CalorieBreakdownController 中显示当天卡路里消耗的脂肪、碳水化合物和蛋白质。

我目前在 CalorieBreakdownController 的所有标签中都得到“0”(如图像最右侧所示)。

我认为问题可能是因为我的 计算委托(delegate) 在 CalorieFooter 单元格中完成。我在单元格中获得小计的方法是将单元格分成几个部分。我对如何从页脚将数据传递给 CalorieBreakdownController 以获取标签中的“小计”有点困惑。

我如何能够从 calorieFooter 单元格中将“subTotal”数据传递给 CalorieBreakdownController(如下图所示)

提前感谢您提供的任何帮助
CalorieTotal

 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")
}
}

最佳答案

使用委托(delegate)可能是一个好主意。我只是写了一些代码(未经测试!)让您了解它是如何工作的:

扩展委托(delegate)类:

protocol CalculationDelegate: class {
func onTouchInfoButton(from cell: CalorieFooter)
func getAdditionalCalories() -> Int
func getCalculations() -> Int // or whatever type this object should be
}

确保您的 View Controller 遵循协议(protocol):
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为 nil时,我添加了默认值0 :
let calories = calculationDelegate?.getAdditionalCalories() ?? 0
let calculations = calculationDelegate?.getCalculations() ?? 0

祝你好运!

关于ios - 如何通过页脚单元格在表格 View 中使用委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59218506/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com