Im trying to calculate the total calories for every section in the section footer
so far Ive failed at getting daily total calorie count results in the footer of every section
there are three test codes that give me 3 different results:
Test Code 1 - puts "0" in every footer
Test Code 2 - gives me something close to an accurate results but adds up the total for all the cells in every section and places the overall total in the footer(s)
Test Code 3 - gives off random numbers as a result in the footer of every section
(as seen in image below)
when im trying to get the results as seen in the far right of the image
Prevent footer overlapping tableViewCell in UITableView - Swift
https://github.com/Dassine/ShoppingCart
import UIKit
class CalorieViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var dailyCalories: [DailyCalories] = []
var groupedCalorieItems: [String: [DailyCalories]] = [:]
var dailySectionTitle: [String] = []
@IBOutlet weak var calorieTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return dailySectionTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let calories = dailySectionTitle[section]
return groupedCartItems[calories]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let calorieCell = tableView.dequeueReusableCell(withIdentifier: "CalorieCell") as! CalorieCell
let calories = dailySectionTitle[indexPath.section]
let caloriesToDisplay = groupedCalorieItems[calories]![indexPath.row]
calorieCell.configure(withCalorieItems: caloriesToDisplay.productList)
return calorieCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let calorieHeader = tableView.dequeueReusableCell(withIdentifier: "CalorieHeader") as! CalorieHeader
let headerTitle = calorieSectionTitle[section]
calorieHeader.dailyTotal.text = "\(headerTitle)"
return calorieHeader
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let calorieFooter = tableView.dequeueReusableCell(withIdentifier: "CalorieFooter") as! CalorieFooter
// Test 1
let sectionTotal = dailyCalories[section].getTotal()
cartFooter.cartTotal.text = "\(sectionTotal)"
// places "0" as total in all the footers cells when passed
// Test 2
let calculate = String(dailyCalories.map{$0.productList.calories}.reduce(0.0, +))
cartFooter.cartTotal.text = "\(calculate)"
// gets overall total for all the cells in every section and places the overall total in every footer
// Test 3
var totalSum: Float = 0
for eachProduct in dailyCalories{
calorieTotalArray.append(eachCalorie.productList.price1)
totalSum = calorieTotalArray.append.reduce(0, +)
calorieFooter.calorieTotal.text = String(totalSum)
}
// gives different totals in each section. But its not accurate and it constantly changes when scrolling up and down
return cartFooter
}
}
class CalorieTracker {
private(set) var items : [DailyCalories] = []
}
extension CalorieTracker {
var total: Float {
get { return items.reduce(0.0) { value, item in
value + item.subTotal
}
}
}
var totalQuantity : Int {
get { return Int(items.reduce(0) { value, item in
value + item.subTotal
})
}
}
}
class DailyCalories {
var productList : ProductList!
var addCalorieCells = [ProductList]()
var subTotal : Float { get { return Float(selectedCalorie) * Float(productList.count) } }
var selectedCalorie: Int!
init(productList: ProductList) {
self.productList = productList
}
func selectedCalorieItem() {
selectedCalorie = Int(Float(productList.calorie))
}
func getTotal() -> Float {
var total: Float = 0
for item in self.addCalorieCells{
total = total + Float(Float(item.count) * item.calorie)
}
return total
}
}
You need to change your model and segregate your model sections index wise OR in your viewDidLoad when you are assigning groupedCalorieItems at that time calculate the sections total and store in array!
Below code is for Model change, initialize this like groupedCalorieItems.
below code is only by my assumptions only :P
Class SectionWiseCalories {
var dailyCaloriesArr : [DailyCalories]!
var caloriesTotal : Float!
init(_ products : [ProductList]) {
// map your data here & manage your caloriesTotal
}
}