如何计算切片中的细胞总数?

How do I calculate the total amount of cells in section?

我正在尝试计算部分页脚中每个部分的总卡路里

到目前为止,我未能在每个部分的页脚中获取每日总卡路里计数结果

三个测试代码给出了 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{[=10=].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
    }
}

您需要更改模型并按索引方式分隔模型部分,或者在分配 groupedCalorieItems 时在 viewDidLoad 中计算部分总数并存储在数组中!

以下代码用于模型更改,像 groupedCalorieItems 一样初始化它。

下面的代码只是我的假设而已:P

Class SectionWiseCalories {
      var dailyCaloriesArr : [DailyCalories]!
      var caloriesTotal : Float!

      init(_ products : [ProductList]) {
           // map your data here & manage your caloriesTotal
       }

}