我正在尝试计算页脚的每个部分的总卡路里
到目前为止,我无法在每个部分的页脚中获取每日总卡路里计数结果
共有3个测试代码,可为我提供3种不同的结果:
测试代码1 -在每个页脚中添加“ 0”
测试代码2 -为我提供了接近准确结果的结果,但将每个部分中所有单元格的总数相加,并将总数汇总到页脚中
测试代码3 -在每个部分的页脚中释放随机数
(如下图所示)
当我试图获取图片最右边所示的结果
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
}
}
答案 0 :(得分:1)
当您在分配groupedCalorieItems时,您需要更改模型并在viewDidLoad中明智地分离模型部分索引,然后计算部分总计并存储在数组中!
下面的代码用于模型更改,请像groupedCalorieItems这样初始化。
下面的代码仅是我的假设:P
Class SectionWiseCalories {
var dailyCaloriesArr : [DailyCalories]!
var caloriesTotal : Float!
init(_ products : [ProductList]) {
// map your data here & manage your caloriesTotal
}
}