swift - 更改 CollectionViewCell 中的 UIButton 颜色
问题描述
我有两个班级:ViewController
和PercentCollectionViewCell
。
当我点击 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
}
解决方案
把你的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
操作分配给每个按钮。
推荐阅读
- apache - 使用 htaccess 将请求重写到 404 错误的目录
- java - 如何使用末尾添加的另一个列表中的数据对列表进行排序?
- javascript - 每当 ajax 页面主动刷新原始网页时,我想在不同的网页上显示图标或消息。这可以做到吗?
- swift - 在 AppCode 中打开项目
- javascript - 触发状态改变
- sql - oracle如何查找两个日期之间的月数
- ios - 如何在单元格内容视图中显示标签?
- ms-access - 来自查询的访问报告莫名其妙地增加了列
- r - 使用西班牙语字符(á、é、í、ó、ú、ñ 等)将 .sql 读入 R
- jupyter-notebook - 如何在 haproxy 后面运行 jupyter notebook 并公开其内容?