【问题标题】:How to get total in footer cell of cells in sections?如何在部分单元格的页脚单元格中获得总数?
【发布时间】:2020-03-21 04:37:45
【问题描述】:

目前我能够让单元格在页脚单元格部分中将它们的总数相加。但它计算每个单元格的总数,无论它在哪个部分,在所有部分的页脚内。

我仍然无法将选择不同价格的单元格的不同价格(Price1 - 3)相加,并传递给该部分

我用来在 CartFooter 中为 cartFooter.cartTotal.text = "\(String(cart.map{$0.cartItems.price1}.reduce(0.0, +)))" 部分中的单元格加总的代码

以前:

我试图让每个部分中的单元格将它们所在的每个部分的 页脚单元格中的总数相加。

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

对于如何在页脚中获取该部分中单元格的总数

有点卡住了

Adding specific data for each section in 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 中使用它得到的结果

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

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

更新2:

这是我在cellForRowAt 中添加的内容,以便在节页脚中添加总计。它将单元格的数据相加,但在页脚中没有给出准确的总数

    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)
    }

【问题讨论】:

  • 可以分享商品代码吗?
  • 刚刚添加了物品类@AndresGomez
  • 你能重新表述你的问题吗?

标签: ios swift uitableview footer sections


【解决方案1】:

代码很多,我不太确定您的编码错误在哪里。话虽如此:

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.cart.price1}.reduce(0.0, +))
    cartFooter.cartTotal.text = "$\(calculate)"

    return cartFooter
}

您的代码似乎说 let sectionTotal = cart[section].getSectionTotal() 是您要查找的总数(即一个部分内的总数),而您在一个部分中显示 OT,通过总结 String(cart.map{$0.cart.price1}.reduce(0.0, +))

换句话说,如果cartFooter 是在一个部分中显示总数的容器,则应该改为cartFooter.cartTotal.text = "$\(sectionTotal)",不是吗?

如果这不是答案,我建议您在每次实例化 footerView 时设置一个断点,并弄清楚它为什么输出它所输出的内容(即 OT,而不是部分总数)。

【讨论】:

  • 是的,“换句话说,如果 cartFooter 是显示部分内总数的容器,则应该改为使用 cartFooter.cartTotal.text = "$(sectionTotal)",不是吗?”将是使其工作的最佳方法,但到目前为止,无论我做什么,它都没有成功
  • 哦,男孩... @Evelyn,您的问题不是显示问题,该部分工作正常。您的问题是模型问题,并且与它进行内部计算的方式有关。您的问题中有太多信息,与 TableView 相关的所有部分与您描述的问题没有任何关系。非常非常令人困惑....对于我的信息,顶部第一张图片的右侧有正确的总数,但不会显示正确的价格(1-3),我正确吗?如果是这样,那么您的问题在于getSectionTotal();所以:1/在那里设置一个断点 2/运行和调试,我怀疑价格....
  • ... 选择未通过,因此代码始终运行相同的选项。另外,您的命名令人困惑:全局购物车是购物车实例的集合,每个购物车都包含一个“购物车”属性,这是项目的集合??...这是灾难的根源,因为方法将与类绑定具有不同角色但标识符相同的名称。您应该重新设计模型代码,用唯一的名称命名每个类,根据它们的职责充分分离它们,并创建所有需要的访问器,以帮助您填充您的表视图...
  • 例如 globalCart.total(), globalCart.confirm(), globalCart.checkout(), globalCart.ship.get/set(), globalCart.cancel(product indexPath:IndexPath), globalCart.get(basket indexPath:IndexPath) & set(...), globalCart.total(indexPath:IndexPath) etc... 以及所有 KVO 观察者,以便在修改部分时更新模型,以及何时模型被更新(在视图之外),它通知 TableView。看看github.com/Dassine/ShoppingCart。小心,还有很多其他的例子没有实现。祝你好运。
  • 最后,如果你真的下定决心要按照个人资料的座右铭学习,你应该看看udemy.com/course/pirate-bay-ios-swift-3-development-complete(我不知道它好不好),或者类似的东西。店面开发是乏味的,需要非常严格。在 *ss 中这是一个非常痛苦的问题,但幸运的是,完成它所需的所有 OOP 已经存在于某个地方,所以不要重新发明轮子:p,并根据您的需要重新编写现有代码。如果您不为此重复使用(而不是复制)其他人的代码,请确保您做错了。
【解决方案2】:

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

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
}

【讨论】:

    【解决方案3】:

    如果您想正确计算每个部分的总价,您需要为每个部分过滤项目。现在我想你总结了你所有的部分。 对于正确的部分,您需要 cellForRow 方法中的狙击手:

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

    在这里面:

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
             let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
            let sectionTotal = cart[section].getSectionTotal()
            let brand = brands[section]
            let catrItemsToDisplay = // now you need to get associated items with this brand (cart)
        // I think it is can looks like this  =  cartItemsToDisplay[brand]
            let calculate = String(cartItemsToDisplay.cart.reduce(0.0, +))
            cartFooter.cartTotal.text = "$\(calculate)"
    
            return cartFooter
        }
    

    【讨论】:

    • 我在 let calculate = String(cartItemsToDisplay.cart.reduce(0.0, +)) 上遇到错误,说 Value of type '[Cart]?'没有成员“购物车”,我这样做是为了让 cartItemsToDisplay = groupedCartItems[brand] 在 vi​​ewForFooterInSection 中。我将如何获得代码来计算该部分中单元格的总数,稍后我会更新我的代码以显示事情现在是如何运作的
    • 在减少之前删除购物车
    【解决方案4】:

    通过 TableViewDelegate 修改您的模型结构和值设置方式。给出一个简短的提示。请看看是否有帮助:

    class Cart {
       var brandWiseProducts: [BrandWiseProductDetails]!
    
       init() {
           brandWiseProducts = [BrandWiseProductDetails]()
       }
    
       initWIthProducts(productList : [BrandWiseProductDetails]) {
           self.brandWiseProducts = productList 
       }    
    }
    
    
    
    class BrandWiseProductDetails {
          var brandName: String!
          var selectedItems: [Items] 
          var totalAmount: Double or anything
    }
    
    class SelectedItem {
         var image
         var name
         var price
    }
    
    
      Sections:
    
      Cart.brandWiseProducts.count
    
      SectionTitle:
    
      Cart.brandWiseProducts[indexPath.section].brandName
    
      Rows in section
    
      Cart.brandWiseProduct[indexPath.section].selectedItem[IndexPath.row]
    
      Footer:
    
      Cart.brandWiseProduct.totalAmount
    

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多