UICollectionViewCell 中的 UILabel 未正确更新



我在整页UICollectionViewCell中有一个"总价格"UILabel。当我按下添加按钮时,它应该在该单元格 totalPrice 标签上添加特定项目的价格。当我按下减法时,它会减去总价。由于某种原因,第三个或有时是第四个单元格显示的总价错误。我的逻辑是错误的,但我无法确定确切的问题是什么。

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {
var totalPrice = Float()
private var hiddenRows = Set<Int>()
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var totalPrice = Float()
private var hiddenRows = Set<Int>()
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
cell.finalLabel.text = String(totalPrice)
cell.delegate = self
let item = itemsArr[indexPath.row]
cell.set(name: item.name, brand: item.brand, price: item.price)
if hiddenRows.contains(indexPath.row) {
cell.myButton.isHidden = true
cell.removeButton.isHidden = false
}else{
cell.removeButton.isHidden = true
cell.myButton.isHidden = false
}
cell.finalLabel.text = String(totalPrice)
return cell
}

@objc func addTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
hiddenRows.insert(indexPath.row)
cell.removeButton.isHidden = false
let item = itemsArr[indexPath.row]
print(item.price)
totalPrice += Float(item.price) ?? 0
cell.finalLabel.text = String(totalPrice)
}

@objc func removeButtonTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
hiddenRows.insert(indexPath.row)
cell.myButton.isHidden = false
let item = itemsArr[indexPath.row]
totalPrice -= Float(item.price) ?? 0
cell.finalLabel.text = String(totalPrice)
//   collectionView?.reloadData()
}
}

protocol PostCellDelegate {
func removeButtonTapped(cell: PostCell)
func addTapped(cell: PostCell)
func didPressButton(_ tag: Int)
}
class PostCell: UICollectionViewCell {
var delegate: PostCellDelegate?
func set(name: String, brand: String, price: String){
nameLabel.text = name
brandLabel.text = brand
priceLabel.text = price
}
override init(frame: CGRect) {
super.init(frame: frame)
self.myButton.addTarget(self, action: #selector(addButtonTapped(sender:)), for: .touchUpInside)
self.removeButton.addTarget(self, action: #selector(subButtonTapped(sender:)), for: .touchUpInside)
setupCellConstraints()
}
@objc func buttonPressed(_ sender: UIButton) {
delegate?.didPressButton(sender.tag)
}

@objc func addButtonTapped(sender: UIButton){
self.delegate?.addTapped(cell: self)
sender.isHidden = true
}

@objc func subButtonTapped(sender: UIButton){
self.delegate?.removeButtonTapped(cell: self)
sender.isHidden = true 
}
}

你应该删除cellForItem函数中的totalPricehiddenRows变量,也许你可以把它带到外面,只是不在那里。因为每当单元格显示时都会调用它,这意味着它将返回到其初始值为空。

此外,如果您希望表视图反映新数据,则数据源应该是要更新的数据源,以便表视图反映其数据。切勿单独编辑单元格属性,因为它是可重用的。

谢谢。希望这有帮助。

这是因为重用单元格 移除总价

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// remove this line
//var totalPrice = Float()
private var hiddenRows = Set<Int>()
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
//remove this line
//cell.finalLabel.text = String(totalPrice)
cell.delegate = self
let item = itemsArr[indexPath.row]
cell.set(name: item.name, brand: item.brand, price: item.price)
if hiddenRows.contains(indexPath.row) {
cell.myButton.isHidden = true
cell.removeButton.isHidden = false
}else{
cell.removeButton.isHidden = true
cell.myButton.isHidden = false
}
// remove this line
//cell.finalLabel.text = String(totalPrice)
return cell

}

在单元格中保留当前价格

class PostCell: UICollectionViewCell {
var currentPrice: Float = 0
var delegate: PostCellDelegate?
func set(name: String, brand: String, price: String){
nameLabel.text = name
brandLabel.text = brand
priceLabel.text = price
finalLabel.text = "(currentPrice)"
}

. . .

}

计算当前价格

@objc func addTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
hiddenRows.insert(indexPath.row)
cell.removeButton.isHidden = false
let item = itemsArr[indexPath.row]
print(item.price)
cell.currentPrice += Float(item.price) ?? 0
cell.finalLabel.text = String(cell.currentPrice)
}

@objc func removeButtonTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell)  else {return}
hiddenRows.insert(indexPath.row)
cell.myButton.isHidden = false
let item = itemsArr[indexPath.row]
cell.currentPrice -= Float(item.price) ?? 0
cell.finalLabel.text = String(cell.currentPrice)
}

最新更新