UPDATE:
目前,我无法在单元脚单元部分中将单元格的总数相加。但是,无论在哪个节中,在所有节的页脚内,它都会计算每个单元的总数。
对于选择了不同价格的单元格,我仍然无法将不同的价格(价格1-3)加起来并传递给该部分
用于在cartFooter.cartTotal.text = "\(String(cart.map{$0.cart.price1}.reduce(0.0, +)))"
部分中的单元格的CartFooter中添加总计的代码im>
上一个:
im试图获取每个部分中的单元,以将它们所在的每个部分的总和添加到footer cell
中。CartVC中的数据是从另一个VC(HomeVC)填充的。这就是为什么[[CartCell
中存在3种不同价格选项的原因,以供数据填充单元格时使用。 只是停留在如何获得该节中单元格的footer
中的总数上>Adding specific data for each section in UITableView - Swift预先感谢,非常感谢您的帮助
class CartViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var selectedProduct: Items!
var cart: [Cart] = []
var groupedCartItems: [String: [Cart]] = [:]
var brands: [String] = []
@IBOutlet weak var cartTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
cartTableView.dataSource = self
cartTableView.delegate = self
groupedCartItems = Dictionary(grouping: cart, by: {$0.brand})
brands = groupedCartItems.map{$0.key}.sorted()
}
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.cart)
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.cart.price1}.reduce(0.0, +))
cartFooter.cartTotal.text = "$\(calculate)"
return cartFooter
}
}
class CartCell: UITableViewCell { @IBOutlet weak var productImage: UIImageView! @IBOutlet weak var productPrice: UILabel! @IBOutlet weak var productName: UILabel! func configure(withCartItems cartItems: Items) { productImage.sd_setImage(with: URL(string: cartItems.imageUrl)) productName.text = "\(cartItems.brand): \(cartItems.name) ● \(cartItems.weight1)" let formatter = NumberFormatter() formatter.maximumFractionDigits = 2 formatter.numberStyle = .decimal if cartItems.selectedOption == 1 { productPrice.text = "$\(formatter.string(for: cartItems.price1)!)" } else if cartItems.selectedOption == 2 { productPrice.text = "$\(formatter.string(for: cartItems.price2)!)" } else if cartItems.selectedOption == 3 { productPrice.text = "$\(formatter.string(for: cartItems.price3)!)" } } }
class Cart { var cart: Items! init(cart: Items) { self.cart = cart } } class Items { var id: String var name: String var brand: String var price1: Float var price2: Float var price3: Float var weight1: String var weight2: String var weight3: String var imageUrl: String var selectedOption: Int init(id: String, name: String, brand: String, price1: Float, price2: Float, price3: Float, weight1: String, weight2: String, weight3: String, imageUrl: String, selectedOption: Int) { self.id = id self.name = name self.brand = brand self.price1 = price1 self.price2 = price2 self.price3 = price3 self.weight1 = weight1 self.weight2 = weight2 self.weight3 = weight3 self.imageUrl = imageUrl self.selectedOption = selectedOption } convenience init(dictionary: [String : Any]) { let id = dictionary["id"] as? String ?? "" let name = dictionary["name"] as? String ?? "" let brand = dictionary["brand"] as? String ?? "" let price1 = dictionary["price1"] as? Float ?? 0.0 let price2 = dictionary["price2"] as? Float ?? 0.0 let price3 = dictionary["price3"] as? Float ?? 0.0 let weight1 = dictionary["weight1"] as? String ?? "" let weight2 = dictionary["weight2"] as? String ?? "" let weight3 = dictionary["weight3"] as? String ?? "" let imageUrl = dictionary["imageUrl"] as? String ?? "" let selectedOption = dictionary["selectedOption"] as? Int ?? 00 self.init(id: id, name: name, brand: brand, price1: price1, price2: price2, price3: price3, weight1: weight1, weight2: weight2, weight3: weight3, imageUrl: imageUrl, selectedOption: selectedOption) } }
更新:目前,我无法在页脚单元部分中获取单元格的总和。但是,无论在哪个节中,在所有节的页脚内,它都会计算每个单元的总数。 ...
let brand = brands[indexPath.section]
let cartItemsToDisplay = groupedCartItems[brand]![indexPath.row]
cartCell.configure(withCartItems: cartItemsToDisplay.cart)