2016-04-04 16 views
0

self.totalPriceLabel節目的所有店鋪Product.It的總價工作正常,但是,當我滾動cell的熄滅屏幕由於dequeueReusableCellWithIdentifierself.totalPriceLabel獲得了不正確value.i現在的儲蓄存儲在NSUserDefaults在數組值。如何在swift中用dequeueReusableCellWithIdentifier在UITableView中管理購物車?

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得到錯誤values.for self.totalPriceLabel

self.totalPriceLabel.text =字符串( 「((self.totalPriceLabel.text!爲 的NSString).doubleValue + nextItemPrice)」)

如何獲得cell值剛剛熄滅關閉屏幕?如何解決這個問題,由於滾動?

+0

我覺得管理'totalPriceLabel'的方法不對,你應該把'totalPrice'放在一個變量中,當變量值更新時你需要更新標籤文本。 – amorbytes

+0

@amorbytes。是的,我在viewcontroller的view.it的底部使用了單個變量totalPriceLabel,而不是在Tableview中 –

回答

3

cellForRowAtIndexpath是做錯計算的錯誤地方。你假設iOS會爲每個單元調用一次這個函數。情況並非如此。這個函數被調用來顯示一個單元格。當你上下滾動時,它可以很好地被調用多次。

當您從基礎數據(self.readArray)中添加或刪除項目時,您應該更新該總數。

此外,添加代碼以更改點擊數量按鈕時的總數。

如果您想要更具體的幫助,請發佈整個控制器。

相關問題