在 tableviewcells 中进行计算并在 UILabel 中进行总结的最佳方法
Best Approach to make Calculation in tableviewcells and sums up in UILabel
我的购物车是这样的
计算所有单元格并汇总总金额标签的最佳方法应该是什么
购物车是这样工作的:
单元格项目的增量使价格标签的值加倍,但是当我将新单元格出列时它已经具有该增量值
尝试使用自定义委托时,委托总是显示 nil
我该怎么办?为什么我的代表总是零?
TableViewCell
class ShoppingCartCell: UITableViewCell {
@IBOutlet weak var cellView:UIView!
@IBOutlet weak var productImageView:UIImageView!
@IBOutlet weak var productName:UILabel!
@IBOutlet weak var brandName:UILabel!
@IBOutlet weak var productPrice:UILabel!
@IBOutlet weak var modifier1Lbl:UILabel!
@IBOutlet weak var modifier2Lbl:UILabel!
@IBOutlet var counterBtns:[UIButton]!
@IBOutlet weak var counterLbl:UILabel!
var delegate : cellDelegateFunc?
override func layoutMarginsDidChange() {
super.layoutMarginsDidChange()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
productImageView.layer.cornerRadius = productImageView.frame.height / 4
cellView.roundUIViewWithShadow(cornerRadius: 4, shadowColor: .darkGray)
cellView.layer.masksToBounds = false
cellView.layer.shadowColor = UIColor.lightGray.cgColor
cellView.layer.shadowOpacity = 1
cellView.layer.shadowOffset = .zero
}
override func awakeFromNib() {
super.awakeFromNib()
cellView.layer.cornerRadius = cellView.frame.height / 16
productImageView.layer.cornerRadius = productImageView.frame.height / 16
}
@IBAction func counter(_ sender:UIButton){
self.delegate?.countItems(self)
}
}
CartViewController(特定部分)
class ShoppingBagVC: UIViewController , cellDelegateFunc {
func countItems(_ cell: ShoppingCartCell) {
print("print")
}
}
协议
protocol cellDelegateFunc : class {
func countItems(_ cell:ShoppingCartCell)
}
CellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if cartAllData[indexPath.row].deal.data != nil {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! ShoppingCartDealCell
cell.originalPrice = Int(cartAllData[indexPath.row].deal.data!.dealPrice)
cell.productName.text = cartAllData[indexPath.row].deal.data?.dealName
cell.productPrice.text = "Rs.\(String(cell.originalPrice))"
cell.freeItem.text = cartAllData[indexPath.row].deal.data?.freeProduct
cell.productImageView?.sd_setImage(with: URL(string: cartAllData[indexPath.row].deal.data!.imageURL), completed: nil)
return cell
} else {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! ShoppingCartCell
var originalPrice = Int()
var price : Int = 2{
didSet {
cell.productPrice.text = "Rs.\(String(price))"
}
}
var count : Int = 1{
didSet {
cell.counterLbl.text = String(count)
price = originalPrice * count
}
}
if let value = cartAllData[indexPath.row].deal.data?.quantity {
cell.counterLbl.text = String(value)
}
if let value = cartAllData[indexPath.row].product.data?.quantity {
cell.counterLbl.text = String(value)
}
originalPrice = Int(cartAllData[indexPath.row].product.data!.productBasePrice)
cell.productPrice.text = "Rs.\(String(originalPrice))"
cell.productName.text = cartAllData[indexPath.row].product.data?.productName
cell.productImageView?.sd_setImage(with: URL(string: cartAllData[indexPath.row].product.data!.imageURL), completed: nil)
cell.modifier1Lbl.text = cartAllData[indexPath.row].product.data?.modifier1
cell.modifier2Lbl.text = cartAllData[indexPath.row].product.data?.modifier2
return cell
}
}
正如@joakim 在评论中所说,您正在 UI 中进行计算!这不是正确的方法
当 UITableView 滚动时,每个单元格都会因为重复使用而重新加载,并且每个单元格都会因为再次加载而失去其状态。因此您必须将每个单元格的状态存储在模型中,并在每次单元格加载时将其传递给您的单元格。
按照您的要求最佳方法 是使用 ViewModel 或 Presenter 来存储 View 的状态(这里是您的单元格),并在每次加载时为该 View 提供数据(例如在你的 cellForRow) 中存储状态或属性
我的购物车是这样的
计算所有单元格并汇总总金额标签的最佳方法应该是什么
购物车是这样工作的:
单元格项目的增量使价格标签的值加倍,但是当我将新单元格出列时它已经具有该增量值
尝试使用自定义委托时,委托总是显示 nil
我该怎么办?为什么我的代表总是零?
TableViewCell
class ShoppingCartCell: UITableViewCell {
@IBOutlet weak var cellView:UIView!
@IBOutlet weak var productImageView:UIImageView!
@IBOutlet weak var productName:UILabel!
@IBOutlet weak var brandName:UILabel!
@IBOutlet weak var productPrice:UILabel!
@IBOutlet weak var modifier1Lbl:UILabel!
@IBOutlet weak var modifier2Lbl:UILabel!
@IBOutlet var counterBtns:[UIButton]!
@IBOutlet weak var counterLbl:UILabel!
var delegate : cellDelegateFunc?
override func layoutMarginsDidChange() {
super.layoutMarginsDidChange()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
productImageView.layer.cornerRadius = productImageView.frame.height / 4
cellView.roundUIViewWithShadow(cornerRadius: 4, shadowColor: .darkGray)
cellView.layer.masksToBounds = false
cellView.layer.shadowColor = UIColor.lightGray.cgColor
cellView.layer.shadowOpacity = 1
cellView.layer.shadowOffset = .zero
}
override func awakeFromNib() {
super.awakeFromNib()
cellView.layer.cornerRadius = cellView.frame.height / 16
productImageView.layer.cornerRadius = productImageView.frame.height / 16
}
@IBAction func counter(_ sender:UIButton){
self.delegate?.countItems(self)
}
}
CartViewController(特定部分)
class ShoppingBagVC: UIViewController , cellDelegateFunc {
func countItems(_ cell: ShoppingCartCell) {
print("print")
}
}
协议
protocol cellDelegateFunc : class {
func countItems(_ cell:ShoppingCartCell)
}
CellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if cartAllData[indexPath.row].deal.data != nil {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! ShoppingCartDealCell
cell.originalPrice = Int(cartAllData[indexPath.row].deal.data!.dealPrice)
cell.productName.text = cartAllData[indexPath.row].deal.data?.dealName
cell.productPrice.text = "Rs.\(String(cell.originalPrice))"
cell.freeItem.text = cartAllData[indexPath.row].deal.data?.freeProduct
cell.productImageView?.sd_setImage(with: URL(string: cartAllData[indexPath.row].deal.data!.imageURL), completed: nil)
return cell
} else {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! ShoppingCartCell
var originalPrice = Int()
var price : Int = 2{
didSet {
cell.productPrice.text = "Rs.\(String(price))"
}
}
var count : Int = 1{
didSet {
cell.counterLbl.text = String(count)
price = originalPrice * count
}
}
if let value = cartAllData[indexPath.row].deal.data?.quantity {
cell.counterLbl.text = String(value)
}
if let value = cartAllData[indexPath.row].product.data?.quantity {
cell.counterLbl.text = String(value)
}
originalPrice = Int(cartAllData[indexPath.row].product.data!.productBasePrice)
cell.productPrice.text = "Rs.\(String(originalPrice))"
cell.productName.text = cartAllData[indexPath.row].product.data?.productName
cell.productImageView?.sd_setImage(with: URL(string: cartAllData[indexPath.row].product.data!.imageURL), completed: nil)
cell.modifier1Lbl.text = cartAllData[indexPath.row].product.data?.modifier1
cell.modifier2Lbl.text = cartAllData[indexPath.row].product.data?.modifier2
return cell
}
}
正如@joakim 在评论中所说,您正在 UI 中进行计算!这不是正确的方法
当 UITableView 滚动时,每个单元格都会因为重复使用而重新加载,并且每个单元格都会因为再次加载而失去其状态。因此您必须将每个单元格的状态存储在模型中,并在每次单元格加载时将其传递给您的单元格。
按照您的要求最佳方法 是使用 ViewModel 或 Presenter 来存储 View 的状态(这里是您的单元格),并在每次加载时为该 View 提供数据(例如在你的 cellForRow) 中存储状态或属性