首页 > 解决方案 > 更改 CollectionViewCell 中的 UIButton 颜色

问题描述

我有两个班级:ViewControllerPercentCollectionViewCell

当我点击 abutton1_Tap时,颜色变为蓝色。当我点击 abutton2_Tap时,第二个按钮的颜色变为蓝色,但第一个按钮的颜色不会变回白色。

PercentCollectionViewCell 类:

class PercentCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var firstPercent: UIButton!
    @IBOutlet weak var secondPercent: UIButton!
}

视图控制器类:

let cellIdentifiers:[String] = ["FirstPercentCell", "SecondPercentCell"]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return cellIdentifiers.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifiers[indexPath.item], for: indexPath) as? PercentCollectionViewCell

    collectionCell?.firstPercent?.addTarget(self, action: #selector(button1_Tap(_:)), for: .touchUpInside)
    collectionCell?.secondPercent?.addTarget(self, action: #selector(button2_Tap(_:)), for: .touchUpInside)

    return collectionCell!
}

@objc func button1_Tap(_ sender: UIButton) {
    let firstPercent = sender

    firstPercent.isSelected = true;
    firstPercent.backgroundColor = UIColor.blue

    secondPercent.isSelected = false;
    secondPercent.backgroundColor = UIColor.white

    percentage = 0.05
}

@objc func button2_Tap(_ sender: UIButton) {
    let firstPercent = sender

    firstPercent.isSelected = false;
    firstPercent.backgroundColor = UIColor.white

    secondPercent.isSelected = true;
    secondPercent.backgroundColor = UIColor.blue

    percentage = 0.1
} 

标签: swift

解决方案


把你的button_Tap函数放在你的UICollectionViewCell子类中。您只需一个button_Tap动作即可。将两个按钮都连接到此功能:

class PercentCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var firstPercent: UIButton!
    @IBOutlet weak var secondPercent: UIButton!

    @objc func button_Tap(_ sender: UIButton) {
        let buttonSelected = sender
        let buttonUnselected = (sender === firstPercent) ? secondPercent : firstPercent

        buttonSelected.isSelected = true
        buttonSelected.backgroundColor = .blue

        buttonUnselected.isSelected = false
        buttonUnselected.backgroundColor = .white

        percentage = (sender === firstPercent) ? 0.05 : 0.1
    }
}

然后将按钮连接到情节提要中,或者像这样设置它们cellForItemAt

collectionCell?.firstPercent?.addTarget(collectionCell!, action: #selector(PercentCollectionViewCell.button_Tap(_:)), for: .touchUpInside)
collectionCell?.secondPercent?.addTarget(collectionCell!, action: #selector(PercentCollectionViewCell.button_Tap(_:)), for: .touchUpInside)

对不起,我忘了澄清每个按钮点击功能的百分比值。所以我想我需要一些功能。同样在未来,还会有更多按钮(最多 10 个)。

在这种情况下,请将您的按钮分配给一个@IBOutlet集合并tag在 range 中分配唯一值0...9。将每个按钮连接到 Storyboard 中的插座集合。

@IBOutlet var buttons: [UIButton]!

然后

@objc func button_Tap(_ sender: UIButton) {
    buttons.forEach { button in button.backgroundColor = .white }
    sender.backgroundColor = .blue
    percentage = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5][sender.tag]
}

确保将button_Tap操作分配给每个按钮。


推荐阅读