gpt4 book ai didi

ios - 第一次触摸后隐藏和取消隐藏 UIButtons

转载 作者:行者123 更新时间:2023-11-30 10:25:27 24 4
gpt4 key购买 nike

我在 UICollectionViewCell 内有 2 个 UIButton,当按下添加按钮时,该项目的价格将添加到totalPrice,并且该按钮变为隐藏,并且“删除”按钮变为事件状态。当按下“删除”按钮时,我希望从总价格中删除该商品的价格,并隐藏“删除”按钮,并激活“添加”按钮。它用于在结帐流程中添加和删除项目。现在,价格会减去并添加到totalPrice,但是按“删除”按钮后,该按钮仍保留在屏幕上。因此多次按下它会导致总价格为负数。

    class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {

var totalPrice = Double()
private var hiddenRows = Set<Int>()


@objc func addPressed(_ sender : UIButton){
hiddenRows.insert(sender.tag)
let item = itemsArr[sender.tag].price
totalPrice += Double(item) ?? 0
sender.isHidden = true
collectionView?.reloadData()
print(item)
print(totalPrice)
}



@objc func buttonTapped(cell: PostCell) {
cell.myButton.isHidden = false
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell

let item = itemsArr[indexPath.row]
cell.delegate = self
cell.set(name: item.name, brand: item.brand, price: item.price)
cell.myButton.tag = indexPath.row


cell.removeButton.addTarget(self, action: #selector(buttonTapped(cell:)), for: .touchUpInside)
print(item)
return cell
}


protocol PostCellDelegate {
func buttonTapped(cell: PostCell)
}

class PostCell: UICollectionViewCell {
var delegate: PostCellDelegate?

let myButton: UIButton = {
let button = UIButton(type: .system)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18)
button.setTitle("Add", for: .normal)
button.backgroundColor = .red
// button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) //here....
return button
}()

let removeButton: UIButton = {
let button = UIButton(type: .system)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.setTitle("Remove", for: .normal)
return button
}()

override func prepareForReuse() {
super.prepareForReuse()
self.delegate = nil
}
func set(name: String, brand: String, price: String){
nameLabel.text = name
brandLabel.text = brand
priceLabel.text = price

}
override init(frame: CGRect) {
// self.delegate = nil
super.init(frame: frame)
self.contentView.addSubview(containerView)
containerView.addSubview(nameLabel)
containerView.addSubview(brandLabel)
containerView.addSubview(priceLabel)
containerView.addSubview(myButton)
containerView.addSubview(removeButton)
containerView.addSubview(finalLabel)
setupCellConstraints()


}

func someButtonTapped(sender: UIButton){
self.delegate?.buttonTapped(cell: self)
}

最佳答案

简单地设置可重用单元中对象的 isHidden 属性并不是一个好主意。您还需要更新 dataSource(Array) 以匹配 TableView 上的单元格。在这种情况下,您可以在 item 模型中添加 isAdded 属性,并在触摸按钮时更新其值。并在cellForRowAt中设置,

cell.myButton.isHidden = !item.isAdded
cell.removeButton.isHidden = item.isAdded

这样,当调用reloadData时,tableView将根据您的数据源(即数组)进行更新。

额外:模型属性建议

struct Item {
let name: String
let brand: String
let price: Double
var isAdded: Bool = false
}

注意:第一次回答,解释不太清楚,抱歉。 xD

关于ios - 第一次触摸后隐藏和取消隐藏 UIButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com