【发布时间】:2020-02-24 19:22:24
【问题描述】:
I have 3 buttons in the cells of my tableview, they buttons are in a @IBAction button collection so when one is selected it turns the button color from blue to red and deselects the button previously pressed to back to blue.执行这些操作时代码运行良好
我遇到的问题是,当在一个单元格中选择一个按钮时,会在另一个单元格中选择完全相同的按钮,如下所示▼
到目前为止,我尝试的方法不起作用,我认为可能起作用的是我在视图控制器中创建一个“@objc func”,但我不知道从我创建的内容中去哪里以防止“镜像”在细胞中
我知道我接近解决方案 提前感谢您提供的任何帮助
How to update UILabel on a button click in UITableViewCell in swift 4 and xcode 9?
import UIKit
class Cell: UITableViewCell {
@IBOutlet weak var lbl1: UILabel!
@IBOutlet weak var lbl2: UILabel!
@IBOutlet weak var lbl3: UILabel!
@IBOutlet weak var btn1: RoundButton!
@IBOutlet weak var btn2: RoundButton!
@IBOutlet weak var btn3: RoundButton!
var lastSelectedButton = UIButton()
@IBAction func cartTypeSelected(_ sender: RoundButton) {
lastSelectedButton.isSelected = false; do {
self.lastSelectedButton.backgroundColor = UIcolor.blue
} //Plus any deselect logic for this button
lastSelectedButton = sender //If any buttons are not affect by this selection logic exclude them here
sender.isSelected = true; do {
self.lastSelectedButton.backgroundColor = UIColor.red
}
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
var lastSelectedButton = UIButton()
@objc func selectedButton(_ sender: RoundButton) {
lastSelectedButton.isSelected = false; do {
self.lastSelectedButton.backgroundColor = UIcolor.blue
} //Plus any deselect logic for this button
lastSelectedButton = sender
sender.isSelected = true; do {
self.lastSelectedButton.backgroundColor = UIColor.red
}
}
}
extension View[![enter image description here][1]][1]Controller: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }
return cell
}
}
【问题讨论】:
标签: ios swift button colors cell