如何在 UITableView 中使用 dequeueReusableCellWithIdentifier 快速管理購物車? (How to manage shopping cart in UITableView with dequeueReusableCellWithIdentifier in swift?)


問題描述

如何在 UITableView 中使用 dequeueReusableCellWithIdentifier 快速管理購物車? (How to manage shopping cart in UITableView with dequeueReusableCellWithIdentifier in swift?)

我的 self.totalPriceLabel 顯示所有商店產品的總價格。它工作正常但是我什麼時候滾動 cell 由於 dequeueReusableCellWithIdentifier self.totalPriceLabel 得到不正確的值。我將值保存在存儲在 NSUserDefaults 中的數組中。

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ‑> UITableViewCell {

        var cell : CartCell? = tableView.dequeueReusableCellWithIdentifier("cartCell") as! CartCell!
        if(cell == nil)
        {
            cell = CartCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cartCell")
        }

        cell?.itemCount.layer.cornerRadius = 5
        cell?.clipsToBounds = true

        cell?.itemCount.layer.borderWidth = 1
        cell?.itemCount.layer.borderColor = UIColor.blackColor().CGColor
        cell?.itemMinus.tag = indexPath.row
        cell?.itemPlus.tag = indexPath.row
        cell?.itemDelete.tag = indexPath.row

        let key = self.readArray[indexPath.row]

         cell?.itemCount.text = String("\(key.allValues[0])")
        let tupleVar = getProductNameFromCharacter(String("\(key.allKeys[0])"))
        cell?.itemName.text = tupleVar.tempName
        cell?.itemPrice.text = String("\(tupleVar.price)")

        //Actual Logic
        let tempCount = key.allValues[0] as! Double
        let nextItemPrice = (cell!.itemPrice.text! as NSString).doubleValue * tempCount
        self.totalPriceLabel.text = String("\((self.totalPriceLabel.text! as NSString).doubleValue + nextItemPrice)")


        return cell!

    }

問題:當滾動 cell 得到錯誤的值。for self.totalPriceLabel .

self. totalPriceLabel.text = String("((self.totalPriceLabel.text! as NSString).doubleValue + nextItemPrice)")

如何獲取 cell 值熄滅屏幕?如何解決由於滾動導致的這個問題?


參考解法

方法 1:

cellForRowAtIndexpath is the wrong place to do that calculation. You are assuming that iOS will call this function once for each cell. This isn't the case. This function is called to display a single cell. It could very well get called multiple times for the same cell as you scroll up and down.

You should be updating that total when you add or remove items from the underlying data (self.readArray).

Also, add code to change the total when the quantity button is tapped.

If you want more specific help, post the entire controller.

(by Avijit Nagareryantxr)

參考文件

  1. How to manage shopping cart in UITableView with dequeueReusableCellWithIdentifier in swift? (CC BY‑SA 2.5/3.0/4.0)

#shopping-cart #iOS #swift #uitableview






相關問題

PHP PDO - 無法序列化或反序列化 PDO 實例 (PHP PDO - cannot serialize or unserialize PDO instances)

Prestashop 在註冊表單中添加額外字段 (Prestashop Add extra fields to registration form)

接近購物車存儲,帶有 sessionID 的數據庫 (Approach shopping cart storage, database with sessionID)

購物車不工作,我想哭 (Shopping cart won't work and I want to cry)

如何在 UITableView 中使用 dequeueReusableCellWithIdentifier 快速管理購物車? (How to manage shopping cart in UITableView with dequeueReusableCellWithIdentifier in swift?)

如何區分 Rs 和 % 值? (How to differentiate Rs and % values?)

貝寶購物車的運費之一 (One of delivery charge for paypal shopping cart)

ZenCart 中的優惠券問題 (Coupon problem in ZenCart)

PAYPAL 貨幣不起作用 (PAYPAL currency is not working)

RESTful 購物車網絡服務 (RESTful Shopping Cart web service)

Gravity Form 不適用於結帳頁面 (Gravity Form doesn't work with Checkout Page)

帶有對象的數組,單擊按鈕後增加/減少對像中的值之一 (Array with objects, increasing/decreasing one of the value in object after click on button)







留言討論