我的self.totalPriceLabel显示了所有商店Product的总价。它可以正常工作,但是当我滚动cell
关闭屏幕时,由于dequeueReusableCellWithIdentifier
self.totalPriceLabel
获取了不正确的值。我正在将值保存在存储在的数组中NSUserDefaults
。
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 = String(“((((self.totalPriceLabel.text!as NSString).doubleValue + nextItemPrice)”)
如何获得cell
刚刚脱离屏幕的价值?如何解决由于滚动导致的此问题?
cellForRowAtIndexpath是执行该计算的错误位置。您假设iOS将为每个单元调用一次此函数。事实并非如此。调用此函数以显示单个单元格。上下滚动时,同一单元格可能会多次调用。
当您从基础数据(self.readArray)中添加或删除项目时,应该更新该总数。
此外,添加代码以在点击“数量”按钮时更改总数。
如果您需要更多具体的帮助,请发布整个控制器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句