UPDATE:
Currently Im able to get cells to add up their total in the sections Footer Cell. But 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
code im using to add up total in the CartFooter for the Cells in the sections cartFooter.cartTotal.text = "\(String(cart.map{$0.cart.price1}.reduce(0.0, +)))"
PREVIOUSLY:
im trying to get the Cells in each section to add up their total in footer cell for each section that they're in.
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
Adding specific data for each section in UITableView - Swift
Thanks in advance, Your help is much appreciated
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()
cartFooter.cartTotal.text = "\(String(cart.map{$0.cart.price1}.reduce(0.0, +)))"
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)
}
}
from How to get total in footer cell of cells in sections?

No comments:
Post a Comment