У меня есть UITableViewCell
, который содержит кучу UISwitches
. Я хочу, чтобы переключатель включался / выключался в зависимости от того, какую строку выбирает пользователь, данные передаются в мою ячейку, но состояние переключателя не обновляется.
Я использую базовый MVC, Storyboard имеет TableView> TableViewCell> Метка |UISwitch
Контроллер:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var testList = [1,2,3,4,5]
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
table.tableFooterView = UIView()
table.delegate = self
table.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell") as! SwitchCell
//Turn them all on at start
cell.setSwitch(rowSelected: true)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell") as! SwitchCell
cell.setSwitch(rowSelected: false)
}
}
SwitchCell
class SwitchCell: UITableViewCell {
@IBOutlet weak var uiswitch: UISwitch!
@IBOutlet weak var label: UILabel!
func setCell(number: Int){
label.text = String(number)
}
func setSwitch(rowSelected:Bool) {
uiswitch.setOn(rowSelected, animated: true)
}
}
Я знаю, что могу просто сделать UISwitch неразрешимым, ноЯ смотрю на изменение его состояния, когда пользователь выбирает строку.