【发布时间】: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