What Im trying to do is pass data to another view controller through a button in the CalorieFooter cell using a Delegate to pass data in the cell.
I can't successfully pass data through the infoButton in the CalorieFooter cell to show the fat, carbs, and proteins for the calorie consumption of that day in the CalorieBreakdownController.
I am currently getting "0"'s in all labels of the CalorieBreakdownController (as seen in the far right of the image).
I think the issue might be because my calculationDelegate is done in the CalorieFooter cell. and the way I got the subtotal in the cell was by seperating the cells into sections. im a little confused at how to pass data to CalorieBreakdownController from the the footer, to get the "Subtotal" in the labels.
How would I be able to pass the "subTotal" data the CalorieBreakdownController from the calorieFooter cell (as seen in the image below)
thanks in advance for any help that is provided 
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 DailyCalories {
var foodList : FoodList!
init(foodList: FoodList) {
self.foodList = foodList
}
}
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")
}
}
from How to pass using delegates in a tableview through the footer cell?
No comments:
Post a Comment