如何在 swift 中使用 dequeueReusableCellWithIdentifier 管理 UITableView 中的购物车?
How to manage shopping cart in UITableView with dequeueReusableCellWithIdentifier in swift?
我的 self.totalPriceLabel 显示所有商店的总价 Product.It 工作正常但是我什么时候滚动 cell
关闭屏幕由于dequeueReusableCellWithIdentifier
self.totalPriceLabel
变得不正确 value.i 正在将值保存在存储在 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)。
此外,添加代码以在点击数量按钮时更改总数。
如果您需要更具体的帮助,post 整个控制器。
我的 self.totalPriceLabel 显示所有商店的总价 Product.It 工作正常但是我什么时候滚动 cell
关闭屏幕由于dequeueReusableCellWithIdentifier
self.totalPriceLabel
变得不正确 value.i 正在将值保存在存储在 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)。
此外,添加代码以在点击数量按钮时更改总数。
如果您需要更具体的帮助,post 整个控制器。