gpt4 book ai didi

swift - 如何关闭 UITableview 中除我单击的开关之外的所有其他开关

转载 作者:行者123 更新时间:2023-11-30 11:39:14 24 4
gpt4 key购买 nike

我有一个 tableView,其中有一个 UISwitch 我正在使用 notificationCenter 来获取 UISwitch 的行。现在,我如何使用该信息来关闭除我单击的开关之外的所有其他开关?

func creatNotification()  {
let switchNotification = Notification.Name("answer")

NotificationCenter.default.addObserver(self,selector: #selector(getSwitchRow),name:switchNotification, object:nil)

}
@objc func getSwitchRow(notification: NSNotification){
rowNumber = notification.object as! Int
print("dataView2, getSwitchRow, rowNumber:", rowNumber)
}

@IBAction func `switch`(_ sender: UISwitch) {
var dataViewObject = DataView2()
dataViewObject.creatNotification()
let switchNotification = Notification.Name("answer")
NotificationCenter.default.post(name: switchNotification, object: rowNumber)

let switchNotification2 = Notification.Name("switch")
NotificationCenter.default.post(name: switchNotification, object: answerSwitch.isOn)

if(answerSwitch.isOn == true){

}

}

最佳答案

你可以定义一个属性:var previousSwitch: UISwitch?

然后选择器:

@objc func setOn(sender: UISwitch,indexPath: IndexPath){
if previousSwitch != nil {
previousSwitch?.isOn = false
}
let sw = sender
let isSetOn = sw.isOn
if (isSetOn){
print("on")
}else{
print("off")
}
previousSwitch = sw
}

在单元格中:

sw.addTarget(self, action: #selector(setOn(sender:indexPath:)), for: .valueChanged)
cell?.contentView.addSubview(sw)

关于swift - 如何关闭 UITableview 中除我单击的开关之外的所有其他开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49426540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com