【问题标题】:How to use segue in footer to pass label data to different sections?如何在页脚中使用 segue 将标签数据传递到不同的部分?
【发布时间】:2020-04-09 13:18:51
【问题描述】:

当按下页脚单元格中的按钮时,我已经成功地将 ItemsVC 的标签数据传递给 Fees VC

但是当我试图将表格视图中的部分的标签传递给 FeesVC 时,我似乎无法让它工作

我试图将 ItemsVC 页脚中的小计传递给不同部分的 FeesViewController 标签,而不是只传递一个部分的数据

我已成功从页脚传递数据(按下按钮时)。它成功地能够将总价格从 BrandFooter 传递给 Fees VC,但如果这有任何意义,则无法将数据从右侧部分传递给 FeesVC 我如何能够将正确的标签数据传递给费用按钮被按下

我觉得我真的很接近我的解决方案,我就在某个地方

我认为问题在于 viewForFooterInSection 将小计(值)从该部分传递到 FeesVC

提前感谢您提供的任何帮助

class ItemsViewController: UIViewController {

    var brandItems: [BrandItem] = []
    var groupedBrandItems: [String: [BrandItem]] = [:]
    var brandSectionTitle: [String] = []

    var selectedLabel: String?             // Populates label data in FeesVC

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        groupedBrandItems = Dictionary(grouping: brandItems, by: {$0.products.brandName})
        brandSectionTitle = groupedBrandItems.map{$0.key}.sorted()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        }if let vc = segue.destination as? FeesViewController {
            vc.stringPassed = selectedLabel!            // Populates label data in FeesVC
        }
    }
}

extension ItemsViewController: UITableViewDelegate, UITableViewDataSource{

    func numberOfSections(in tableView: UITableView) -> Int {
        return brandSectionTitle.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let brand = brandSectionTitle[section]
        return groupedBrandItems[brand]!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let brandCell = tableView.dequeueReusableCell(withIdentifier: "BrandCell") as! BrandCell

        let brand = brandSectionTitle[indexPath.section]
        let brandItemsToDisplay = groupedBrandItems[brand]![indexPath.row]
        brandCell.configure(withCartItems: brandItemsToDisplay.products)

        return brandCell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let brandHeader = tableView.dequeueReusableCell(withIdentifier: "BrandHeader") as! BrandHeader

        let headerTitle = brandSectionTitle[section]
        brandHeader.brandName.text = "Brand: \(headerTitle)"

        return brandHeader
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let brandFooter = tableView.dequeueReusableCell(withIdentifier: "BrandFooter") as! BrandFooter

        let brand = brandSectionTitle[section]
        let arrAllItems = groupedCartItems[brand]!
        var subtotal: Float = 0
        for item in arrAllItems {
            if item.products.selectedOption == 1 {
                subtotal = subtotal + (Float(item.products.price) * Float(item.products.count))
            }

        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .currency
        let total = numberFormatter.string(from: NSNumber(value: Float(subtotal)))

        brandFooter.totalPrice.text = String(Total!)

        //trying to pass each section subtotal to FeesVC
        self.selectedLabel = "\(subtotal)"  // passes code to FeesVC

        return brandFooter
    }

}

class FeesViewController: UIViewController {

    @IBOutlet weak var feesView: UIView!

    @IBOutlet weak var subtotalLbl: UILabel!
    @IBOutlet weak var salesTaxLbl: UILabel!
    @IBOutlet weak var totalLbl: UILabel!

    var stringPassed = String()     // Populates label data in FeesVC

    override func viewDidLoad() {
        super.viewDidLoad()

        let tax = Float(stringPassed)! * Float(0.0825)
        let total = Float(stringPassed)! + tax

        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .currency
        let subTotal = numberFormatter.string(from: NSNumber(value: Float(stringPassed)!))
        let salesTax = numberFormatter.string(from: NSNumber(value: Float(tax)))
        let overallTotal = numberFormatter.string(from: NSNumber(value: Float(total)))

        subtotalLbl.text = subTotal
        salesTaxLbl.text = "(\(String(describing: salesTax!)))"
        totalLbl.text = "(\(String(describing: overallTotal!)))"
     }

    @IBAction func returnButton(_ sender: Any) {
        dismiss(animated: true, completion: nil)
        print("Close Taxes and Fees")
    }    
}

更新

每当按下更多信息按钮时,我尝试发布每个部分的小计时,我一直在 FeesVC 中获得相同的小计和计算

self.selectedLabel = "\(subtotal)"

viewForFooterInSection 中的这行代码在按下某个部分的按钮时成功传递了小计,但是当在相应的页脚中按下按钮时,它并没有传递每个部分的小计

【问题讨论】:

  • 所以澄清一下,小计只是从一个单元格的数据计算,而不是得到所有单元格数据的总和?抱歉,试图理解
  • @drfalcoew 我得到了该部分中所有单元格的总和,它更多地显示了 FeesVC 中每个部分的总和,因为我仅获得 FeesVC 中某个部分的总和,很快就会发布一张图片来向您展示我的意思。
  • 嗨,看看我的回答

标签: ios swift tableview segue pass-data


【解决方案1】:

首先声明一个双精度数组来存储每个小计。

var subTotalArray[Double] = [Double]()

然后,当您在 viewForFooterInSection 函数中计算小计时,像这样初始化您创建的数组(根据您的代码和变量名称进行更改):

subTotalArray[section] = subtotal 
// use the local variable "section" to iterate
// and assign the subtotal to the correct section footer

下一步要确定按下了哪个信息按钮以获得正确的小计数据,您可以在 viewForFooterInSection 函数中配置 moreInfo 按钮的 tag 属性。像这样分配标签:

moreInfo.tag = section

最后有了所有这些变量,我们可以使用连接到您的 moreInfo 按钮的处理函数正确分配、确定和收集每个单独的小计。

func handleMoreInfo(sender: UIButton) { 
    selectedLabel = subTotalArray[sender.tag]
}

【讨论】:

    【解决方案2】:

    假设我从您的问题中了解到,您需要将小计传递给其他 VC。

    您做错的是将小计保存在 footerForSection 的一个变量中,该变量将具有最后一个页脚部分的最后一个小计(这就是您的代码中发生的情况)

    self.selectedLabel = "\(subtotal)"  // passes code to FeesVC
    

    编辑

    因此,在您的场景中,您需要将小计保存在数组中,当您点击节页脚时,点击获取节的索引并使用数组获取数据并将其传递给其他 VC

    【讨论】:

    • 我怎样才能创建数组来做到这一点?因为我对如何做到这一点有点困惑?
    • 每当您计算小计时,然后将它们存储在全局数组中,您一定已经处理了水龙头吧?所以当时获取页脚部分的索引并从该特定索引处的数组中获取值,如果您仍然有问题让我知道,我将编辑我的答案然后
    • 谢谢你,这意味着很多❤️我会通宵工作,让你知道结果。我了解您为此提供的方法,这只是我如何弄清楚如何使其发挥作用的方法,哈哈
    • 是的,我一直在尝试仍然没有成功获得任何足够的结果,无论我尝试了什么,它仍然不起作用。我将如何使这个全局数组工作,因为我真的需要帮助来完成这项工作。感谢您对我的关注,您真的很乐意为我服务。
    • 你能帮我解决这个问题吗,我仍然有一个问题试图找出你的解决方案
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多