我如何关闭 UITableview 中除我单击的那个之外的所有其他开关
How do i turn of all other switches in a UITableview except the one i clicked
我有一个 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)
我有一个 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)