ios - 如何计算部分中的单元格总数?
问题描述
我试图计算部分页脚中每个部分的总卡路里
到目前为止,我未能在每个部分的页脚中获得每日总卡路里计数结果
三个测试代码给了我 3 个不同的结果:
测试代码 1 - 在每个页脚中放置“0”
测试代码 2 - 为我提供了接近准确结果的结果,但将每个部分中所有单元格的总数加起来,并将总总数放在页脚中
测试代码 3 - 在每个部分的页脚中发出随机数作为结果
(如下图所示)
当我试图获得图像最右边的结果时
防止 UITableView 中的页脚重叠 tableViewCell - 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
}
}
解决方案
您需要更改模型并在您的 viewDidLoad 中以索引方式分离模型部分,或者在您分配 groupedCalorieItems 时计算部分总数并存储在数组中!
下面的代码用于模型更改,像 groupedCalorieItems 一样初始化它。
下面的代码只是我的假设:P
Class SectionWiseCalories {
var dailyCaloriesArr : [DailyCalories]!
var caloriesTotal : Float!
init(_ products : [ProductList]) {
// map your data here & manage your caloriesTotal
}
}
推荐阅读
- javascript - 通过其键和值(js)来自另一个对象的新对象
- javascript - 如何使用 Nuxt Js 将数据属性添加到脚本标签?
- biztalk - 在 BizTalk 2020 中禁用证书验证
- python - python-pptx 如何将页面上边距设置为 0
- c++ - 卡萨布兰卡 cpprestsdk
- javascript - 将鼠标悬停在按钮上时,使按钮内的 materialUI 图标旋转(使用动画)
- docker - Google Cloud 容器注册表/工件注册表权限
- hazelcast - 如何使用 Hazelcast 作为 bazel 的缓存后端
- java - 如何使用 Swing 计时器来延迟动作
- python - python dijkstra的算法使用类实现的可能问题