【发布时间】:2021-04-05 07:19:56
【问题描述】:
我将尝试使我的尝试在写作中有意义。
我为 tableview 单元格和 uiswitch 使用了代码而不是情节提要。单元格的数量基于计数。如何选择不同单元格内的 uiswitch 的值更改时发生的情况。
我觉得我需要函数 didselectrow 但我不知道如何访问它的 uiswitch。正如您在 handleswitchaction 函数中看到的那样,每个 uiswitch 都会执行操作。
class SettingsCell: UITableViewCell {
lazy var switchControl: UISwitch = {
let switchControl = UISwitch()
switchControl.isOn = true
switchControl.onTintColor = UIColor(red: 55/255, green: 130/255, blue: 250/255, alpha: 1)
switchControl.translatesAutoresizingMaskIntoConstraints = false
switchControl.addTarget(self, action: #selector(handleSwitchAction), for: .valueChanged)
return switchControl
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(switchControl)
switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -12).isActive = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//fatalError("init(coder:) has not been implemented")
}
@objc func handleSwitchAction(sender: UISwitch) {
if sender.isOn {
print("Is on")
} else {
print("Is off")
}
}
}
【问题讨论】:
标签: ios swift uitableview uiswitch