So it appears that a Table View in Swift dynamically reloads data onto the screen based on the scroll position. However, in my case, I don't want the mechanism to work this way since the items in my table view update the total price of the user's order and is unnecessarily screwing up the total price value. When I scroll up or down, the cells in which the reload data is being called on are reperforming the math and is leading to the incorrect total price.
My wish is to recalculate the price using the reloadData()
function only when the UIStepper is pressed on a particular cell rather than on scrolling also. I've done this by having an IBAction function that calls reloadData
when the stepper is pressed. The problem with the math and internal data occurs when the table view is scrolled on and is repeatedly calling the reloadData
function as the visible cells are changed which I don't want it to do for any form of scrolling whatsoever.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let add_on = JSON(self.add_ons[indexPath.row])
let cell = tableView.dequeueReusableCell(withIdentifier: "AddOnCell", for: indexPath) as! AddOnTableViewCell
quantities[indexPath.row] = cell.quantity
//cell.quantityLabel.text = String(quantities[indexPath.row])
let price = Double(add_on["price"].int!)/100.0
let quantity:Double = Double(quantityLabel.text!)!
//when user tries to increase add-on quantity beyond ticket quantity
if(Int(quantity) < cell.quantity){
print("QUANTITY UPDATED")
cell.quantity -= 1
quantities[indexPath.row] = cell.quantity
}
self.addOnPrice = updateTotal(quantities: self.quantities)
totalPriceLabel.text = (Double(event_subtotal * quantity) + addOnPrice).dollarRound()
cell.addonLabel?.text = add_on["name"].string! + " (+$\(price.roundTo(places: 2))" + ")"
return cell
}
func updateTotal(quantities: [Int]) -> Double{
var result:Double = 0.0
for i in 0..<quantities.count{
let curr_addon = JSON(self.add_ons[i])
let price = Double(curr_addon["price"].int!)/100.0
result += Double(quantities[i]) * price
}
return result.dollarRoundDouble()
}
@IBAction func stepperClicked(_ sender: UIStepper) {
self.addOnPrice = 0.0
AddOnTableView.reloadData()
}
Below is the cell data model:
class AddOnTableViewCell: UITableViewCell{
@IBOutlet weak var addonLabel: UILabel!
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var quantityStepper: UIStepper!
var quantity : Int = 0 {
didSet{
self.quantityLabel.text = String(quantity)
self.quantityStepper.value = Double(quantity)
}
}
//ref: https://stackoverflow.com/questions/42876739/swift-increment-label-with-stepper-in-tableview-cell
@IBAction func quantityStep(_ sender: UIStepper) {
self.quantity = Int(sender.value)
self.quantityLabel.text = String(quantity)
}
}
Behavior: Upon scrolling, the total price calculated is changing without stepper interaction. Based on scroll position, the quantity for 1 item is "exchanging" the quantity value with another item. First item quantity becoming 1 updates last item's quantity for some reason. Unable to debug this behavior.