![](/img/trans.png)
[英]SwiftUI - How to change the colour of all other buttons when pressing on button?
[英]Reset all buttons after pressing the next button
单击按钮后,我需要将图像从按钮更改为另一个。 我在 setBackgroundImage() 的帮助下做到了这一点。 但是如何一次更改所有按钮的图像并将图像仅更改为按下的按钮... ![button set][1]
我的视图控制器
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableView.separatorStyle = .none
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UIT
cell.TitleLabel.text=dataMas[indexPath.row]
cell.name.text = titleTrack[indexPath.row]
cell.btns.tag = indexPath.row
cell.btns.addTarget(self, action: #selector(ViewController.PlayTrack(_:)), for: UIControlEvents.touchUpInside )
return cell
}
和细胞类
导入 UIKit
类 UIT:UITableViewCell {
@IBOutlet weak var btns: UIButton!
@IBOutlet weak var TitleLabel: UILabel!
@IBOutlet weak var name: UILabel!
override func awakeFromNib() {
}
override func setSelected(_ selected: Bool, animated: Bool) {
}
}
在你的 ViewController.PlayTrack 中试试这个:
var numberOfButtons: Int = 5
@IBOutlet weak var btns: UIButton!
for i in 1...numberOfButtons {
if btns.tag == i {
// SET IMAGE TO BTN HERE.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.