在CollectionViewCell中更改UIButton颜色

时间:2018-07-21 12:42:30

标签: swift

我有两个班级:ViewControllerPercentCollectionViewCell

当我点击button1_Tap时,颜色变为蓝色。当我点击button2_Tap时,第二个按钮的颜色变成蓝色,但是第一个按钮的颜色没有变回白色。

PercentCollectionViewCell类:

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

ViewController类:

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
} 

1 个答案:

答案 0 :(得分:2)

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范围内的唯一0...9值。将每个按钮连接到情节提要中的插座集合。

@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操作。