我在表視圖的每個單元格中都添加了一個開關,但開關功能沒有被呼叫。如果我在前臺頁面給開關,它就會成功顯示。但在表視圖單元格中,它不作業`
。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = models[indexPath.row].Address。
cell.textLabel?.text = models[indexPath.row].Number。
cell.textLabel?.text = models[indexPath.row].Role。
cell.textLabel?.text = models[indexPath.row].Name.
//switch
let mySwitch = UISwitch( frame: .zero)
mySwitch.setOn(false, animated: true)
mySwitch.tag = indexPath.row
mySwitch.tintColor = UIColor.red
mySwitch.onTintColor = UIColor.green
mySwitch.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
cell.accessoryView = mySwitch
return cell。
}
@IBAction func switchValueDidChange(_sender: UISwitch){
if _sender .isOn{
print("switch on"/span>)
view.backgroundColor = UIColor.red }
else{
view.backgroundColor = UIColor.systemPurple
}
}
`
uj5u.com熱心網友回復:
簽名是錯誤的。在下劃線和sender之間必須有一個空格字符。如果它不是一個真正的IBAction,請將@IBAction替換為@objc
@objc func switchValueDidChange(_ sender: UISwitch) {
if sender.isOn {...
而且--與這個問題無關--選擇器可以簡單地寫成
#selector(switchValueDidChange)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/306778.html
標籤:
