如何获得单元格的页脚单元格中的总数? [英] How to get total in footer cell of cells in sections?

查看:132
本文介绍了如何获得单元格的页脚单元格中的总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我能够在页脚单元"部分中获取单元格的总和.但是,无论在哪个单元格的哪个部分中,都在所有部分页脚内计算每个单元格的总数.

Currently Im able to get cells to add up their total in the sections Footer Cell. But it calculates the total for every cell no matter what section its in, inside the all the sections footer.

对于选择了不同价格的单元格,我仍然无法为其添加不同的价格(价格1-3)

I still can't get it to add up the different prices(Price1 - 3) for the cells that have a different prices selected passed into it the Section

代码im用于在cartFooter.cartTotal.text = "\(String(cart.map{$0.cartItems.price1}.reduce(0.0, +)))"

上一个:

im试图获取每个部分中的单元格,以将其所在的每个部分的总和添加到页脚单元格中.

im trying to get the Cells in each section to add up their total in footer cell for each section that they're in.

CartVC中的数据是从另一个VC(HomeVC)填充的.这就是为什么当数据填充单元格时, CartCell 中有3种不同的价格选项的原因.

The data in the CartVC is populated from another a VC(HomeVC). Which is why there is 3 different price options in the CartCell for when the data populates the cells.

只是停留在如何获得该部分中单元格的页脚中的总数上

Just kind of stuck on how I would be able to get the total in the footer for the cells in the section

为UITableView中的每个部分添加特定数据-Swift

在此先感谢您的帮助

extension CartViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return brands
    }

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

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

        let brand = brands[indexPath.section]
        let cartItemsToDisplay = groupedCartItems[brand]![indexPath.row]
        cartCell.configure(withCartItems: cartItemsToDisplay.cartItems)

        return cartCell
    }

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

        let headerTitle = brands[section]
        cartHeader.brandName.text = "Brand: \(headerTitle)"

    return cartHeader
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
         let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
        let sectionTotal = cart[section].getSectionTotal()

        let calculate = String(cart.map{$0.cartItems.price1}.reduce(0.0, +))
        cartFooter.cartTotal.text = "$\(calculate)"

        return cartFooter
    }

}

更新:这些是我在CartFooter中使用此结果的结果

Update: these are the results that I am getting using this in the CartFooter

let calculate = String(cart.map{$0.cart.price1}.reduce(0.0, +))
cartFooter.cartTotal.text = "$\(calculate)"

会在试图获取其页脚中每个部分的总计时,计算所有部分的总合计(OT),并将OT放置在所有页脚单元中(如下所示▼) (如右侧▲上方的图片所示)

which calculates the overall total (OT) for all the sections and places the OT in all all Footer Cells(as seen below ▼) when im trying to get the total for each section in their footers (as seen in image above ▲ on the right side)

Update2:

这是我在cellForRowAt中添加的内容,以便将总计添加到节的页脚中.它会为单元格添加数据,但页脚中的总数却无法准确

this what ive added in my cellForRowAt to get the totals to add up in the section footer. it adds up the data for the cells but it doesn't give an accurate total in the footer

    var totalSum: Float = 0

    for eachProduct in cartItems{
        productPricesArray.append(eachProduct.cartItem.price1)
        productPricesArray.append(eachProduct.cartItem.price2)
        productPricesArray.append(eachProduct.cartItem.price3)
        totalSum = productPricesArray.reduce(0, +)

        cartFooter.cartTotal.text = String(totalSum)
    }

推荐答案

@Evelyn直接在页脚中尝试计算.尝试下面的代码.

@Evelyn Try calculation directly in footer. Try below code.

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
    let brand = brands[indexPath.section]
    let arrAllItems = groupedCartItems[brand]!
    var total: Float = 0
    for item in arrAllItems {
        if item.selectedOption == 1 {
             total = total + Float(item.price1)
        } else item cartItems.selectedOption == 2 {
             total = total + Float(item.price2)
        } else if item.selectedOption == 3 {
             total = total + Float(item.price3)
        }
    }
    cartFooter.cartTotal.text = "$\(total)"
    return cartFooter
}

这篇关于如何获得单元格的页脚单元格中的总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆