如何通过页脚单元格在表视图中使用委托?

伊芙琳

我试图做的是使用Delegate通过CalorieFooter单元中的按钮将数据传递到另一个视图控制器。

我无法成功通过CalorieFooter单元格中的infoButton传递数据以显示卡路里,卡路里和卡路里中的卡路里,卡路里和蛋白质。

我目前在CalorieBreakdownController的所有标签中都得到了“ 0”(如图像的最右边所示)。

我认为这个问题可能是因为我的calculationDelegate在CalorieFooter细胞来完成。而我得到小计的方法是将这些单元分成几部分。我对如何从页脚将数据传递到CalorieBreakdownController以获得标签中的“小计”感到困惑。

我如何能够从卡路里卡路里单元格传递CalorieBreakdownController的“ subTotal”数据(如下图所示)

在此先感谢您提供的任何帮助 总热量

 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 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")
    }    
}
范德兰

使用委托可能是一个好主意。我只是编写了一些代码(未经测试!)来告诉您它如何工作:

扩展委托类:

protocol CalculationDelegate: class {
    func onTouchInfoButton(from cell: CalorieFooter)
    func getAdditionalCalories() -> Int
    func getCalculations() -> Int // or whatever type this object should be
}

确保您的视图控制器遵循以下协议:

extension CalorieViewController: CalculationDelegate { 
    func onTouchInfoButton(from cell: CalorieFooter) { ... }
    func getAdditionalCalories() -> Int {
        return self.calories
    }
    func getCalculations() -> Int {
        return self.calculations
    }
}

CalorieViewController其中添加存储当前状态的局部变量(卡路里/计算量)

class CalorieViewController: UIViewController {
    private var calories: Int = 0
    private var calculations: Int = 0

    ... other code that is already in the UIViewController
}

确保在某个地方初始化这些变量!就像是:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    ... same as before...

    self.calories = subtotal // I guess?
    self.calculations = calculations // put this somewhere where calculations is initialized
}

现在,数据应该在中可用CalorieFooter我在CalculationDelegate为时添加了默认值0 nil

let calories = calculationDelegate?.getAdditionalCalories() ?? 0
let calculations = calculationDelegate?.getCalculations() ?? 0

祝好运!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何通过快速点击集合视图的单元格在表视图中重新加载数据

来自分类Dev

如何在表视图中创建动态单元格?

来自分类Dev

如何仅使用鼠标在javafx中选择表视图中的多个单元格?

来自分类Dev

如何使用按钮将单元格动态添加到我的表视图中

来自分类Dev

Swift:如何使用NSUserDefaults在表视图中为每个单元格存储单独的信息

来自分类Dev

如何使用表视图中的 where 按钮在表视图单元格之间进行数据转换?

来自分类Dev

通过点击表视图中的单元格以及如何知道哪个单元格已选中或未选中来选中/取消选中复选框

来自分类Dev

如何使用分段控件在一个表视图中维护两个自定义单元格?

来自分类Dev

如何在tvOS中使用嵌套集合视图更改重用表视图单元格中的焦点?

来自分类Dev

如何在表视图中添加额外的行,除了快速的两个单元格

来自分类Dev

当用户单击单元格时,如何从表详细视图中的数组加载视频?

来自分类Dev

使用Swift在表视图中重新加载单元格数据

来自分类Dev

如何在容器视图中更新单元格

来自分类Dev

如何在集合视图中单选单元格?

来自分类Dev

如何使用标签在表格视图中显示单元格数

来自分类Dev

如何在pcolor图中使用单元格数组作为轴刻度?

来自分类Dev

表格视图中重叠的单元格

来自分类Dev

如何通过Segue发送集合视图单元格文本

来自分类Dev

如何在Swift的故事板上的表视图中添加两个自定义单元格?

来自分类Dev

如何在基于节的表视图中获取开关更改事件时单元格的索引路径

来自分类Dev

如何在Swift的故事板上的表视图中添加两个自定义单元格?

来自分类Dev

如何在基于节的表视图中获取开关更改事件的单元格的索引路径

来自分类Dev

如何在 PyQT4 表视图中突出显示每行中具有最高值的单元格

来自分类Dev

在 Swift 中使用 Parse 删除表格视图单元格

来自分类Dev

如何在表格视图单元格中使用查询中的数据?

来自分类Dev

基于先前表视图中点击的单元格的快速动态视图

来自分类Dev

如何通过单击表视图控制器中的单元格来生成视图控制器

来自分类Dev

如何区分原型表视图单元格?

来自分类Dev

表视图按单元格排列的单元格

Related 相关文章

  1. 1

    如何通过快速点击集合视图的单元格在表视图中重新加载数据

  2. 2

    如何在表视图中创建动态单元格?

  3. 3

    如何仅使用鼠标在javafx中选择表视图中的多个单元格?

  4. 4

    如何使用按钮将单元格动态添加到我的表视图中

  5. 5

    Swift:如何使用NSUserDefaults在表视图中为每个单元格存储单独的信息

  6. 6

    如何使用表视图中的 where 按钮在表视图单元格之间进行数据转换?

  7. 7

    通过点击表视图中的单元格以及如何知道哪个单元格已选中或未选中来选中/取消选中复选框

  8. 8

    如何使用分段控件在一个表视图中维护两个自定义单元格?

  9. 9

    如何在tvOS中使用嵌套集合视图更改重用表视图单元格中的焦点?

  10. 10

    如何在表视图中添加额外的行,除了快速的两个单元格

  11. 11

    当用户单击单元格时,如何从表详细视图中的数组加载视频?

  12. 12

    使用Swift在表视图中重新加载单元格数据

  13. 13

    如何在容器视图中更新单元格

  14. 14

    如何在集合视图中单选单元格?

  15. 15

    如何使用标签在表格视图中显示单元格数

  16. 16

    如何在pcolor图中使用单元格数组作为轴刻度?

  17. 17

    表格视图中重叠的单元格

  18. 18

    如何通过Segue发送集合视图单元格文本

  19. 19

    如何在Swift的故事板上的表视图中添加两个自定义单元格?

  20. 20

    如何在基于节的表视图中获取开关更改事件时单元格的索引路径

  21. 21

    如何在Swift的故事板上的表视图中添加两个自定义单元格?

  22. 22

    如何在基于节的表视图中获取开关更改事件的单元格的索引路径

  23. 23

    如何在 PyQT4 表视图中突出显示每行中具有最高值的单元格

  24. 24

    在 Swift 中使用 Parse 删除表格视图单元格

  25. 25

    如何在表格视图单元格中使用查询中的数据?

  26. 26

    基于先前表视图中点击的单元格的快速动态视图

  27. 27

    如何通过单击表视图控制器中的单元格来生成视图控制器

  28. 28

    如何区分原型表视图单元格?

  29. 29

    表视图按单元格排列的单元格

热门标签

归档