You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift:UITableView单元格加减按钮联动总价计算实现求助

Hey there! Let's tackle your UITableView cart calculation and quantity update issue step by step. Here's a clear, actionable solution tailored to your setup:

Step 1: Define a Structured Data Model

First, create a Codable model to represent your cart items—this makes saving/loading from UserDefaults straightforward:

struct CartItem: Codable {
    let productID: String
    let imageName: String // Or use a URL if loading remote images
    var quantity: Int
    let unitPrice: Double
    
    // Calculated property for quick subtotal access
    var subtotal: Double {
        return Double(quantity) * unitPrice
    }
}
Step 2: Set Up Your View Controller

In your UIViewController, add the core properties and initial setup:

class CartViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var cartTableView: UITableView!
    @IBOutlet weak var totalPriceLabel: UILabel!
    
    private var cartItems: [CartItem] = []
    private let userDefaults = UserDefaults.standard
    private let cartStorageKey = "SavedCartItems"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        loadCartData()
        updateTotalPrice()
    }
    
    private func setupTableView() {
        cartTableView.dataSource = self
        cartTableView.delegate = self
        // Register your custom cell (adjust nib name and identifier to match yours)
        cartTableView.register(UINib(nibName: "CartItemCell", bundle: nil), forCellReuseIdentifier: "CartCell")
    }
}
Step 3: Handle UserDefaults Data Loading/Saving

Add methods to persist and retrieve your cart data:

private func loadCartData() {
    guard let savedData = userDefaults.data(forKey: cartStorageKey) else {
        cartItems = [] // Initialize empty if no saved data
        return
    }
    do {
        cartItems = try JSONDecoder().decode([CartItem].self, from: savedData)
    } catch {
        print("Failed to decode cart items: \(error.localizedDescription)")
        cartItems = []
    }
}

private func saveCartData() {
    do {
        let encodedData = try JSONEncoder().encode(cartItems)
        userDefaults.set(encodedData, forKey: cartStorageKey)
    } catch {
        print("Failed to encode cart items: \(error.localizedDescription)")
    }
}
Step 4: Implement TableView Data Source

Populate your cells and wire up the quantity buttons:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cartItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as? CartItemCell else {
        return UITableViewCell()
    }
    
    let currentItem = cartItems[indexPath.row]
    // Populate cell UI elements
    cell.productImageView.image = UIImage(named: currentItem.imageName)
    cell.quantityLabel.text = "\(currentItem.quantity)"
    cell.unitPriceLabel.text = String(format: "$%.2f", currentItem.unitPrice)
    
    // Tag buttons with row index to identify which item to modify
    cell.minusButton.tag = indexPath.row
    cell.minusButton.addTarget(self, action: #selector(minusQuantityTapped(_:)), for: .touchUpInside)
    
    cell.plusButton.tag = indexPath.row
    cell.plusButton.addTarget(self, action: #selector(plusQuantityTapped(_:)), for: .touchUpInside)
    
    return cell
}
Step 5: Handle Quantity Button Taps

Update the item quantity, refresh the cell, recalculate the total, and save changes:

@objc private func minusQuantityTapped(_ sender: UIButton) {
    let rowIndex = sender.tag
    guard rowIndex < cartItems.count else { return }
    
    var updatedItem = cartItems[rowIndex]
    // Prevent quantity from dropping below 1 (adjust to 0 if needed)
    guard updatedItem.quantity > 1 else { return }
    
    updatedItem.quantity -= 1
    cartItems[rowIndex] = updatedItem
    
    // Refresh the specific cell's quantity label
    if let cell = cartTableView.cellForRow(at: IndexPath(row: rowIndex, section: 0)) as? CartItemCell {
        cell.quantityLabel.text = "\(updatedItem.quantity)"
    }
    
    updateTotalPrice()
    saveCartData()
}

@objc private func plusQuantityTapped(_ sender: UIButton) {
    let rowIndex = sender.tag
    guard rowIndex < cartItems.count else { return }
    
    var updatedItem = cartItems[rowIndex]
    updatedItem.quantity += 1
    cartItems[rowIndex] = updatedItem
    
    // Refresh the specific cell's quantity label
    if let cell = cartTableView.cellForRow(at: IndexPath(row: rowIndex, section: 0)) as? CartItemCell {
        cell.quantityLabel.text = "\(updatedItem.quantity)"
    }
    
    updateTotalPrice()
    saveCartData()
}
Step 6: Calculate & Display Total Price

Add a method to compute the sum of all item subtotals:

private func updateTotalPrice() {
    let total = cartItems.reduce(0.0) { runningTotal, item in
        runningTotal + item.subtotal
    }
    totalPriceLabel.text = String(format: "Total: $%.2f", total)
}
Step 7: Custom UITableViewCell Setup

Make sure your cell class has the required outlets connected:

class CartItemCell: UITableViewCell {
    @IBOutlet weak var productImageView: UIImageView!
    @IBOutlet weak var quantityLabel: UILabel!
    @IBOutlet weak var minusButton: UIButton!
    @IBOutlet weak var plusButton: UIButton!
    @IBOutlet weak var unitPriceLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Optional: Style buttons (rounded corners, etc.)
        minusButton.layer.cornerRadius = 4
        plusButton.layer.cornerRadius = 4
    }
}

Quick Notes:

  • If using remote product images, replace UIImage(named:) with an async image loader (like URLSession or a library).
  • Adjust the minimum quantity rule in minusQuantityTapped if you want to allow zero items.
  • Double-check that all IBOutlets in your cell are properly connected to storyboard elements.

内容的提问来源于stack exchange,提问作者Bogdan59

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:12:39