如何读取自定义 UITableViewCell 中 UISwitch 的变化
How to read the changes in UISwitch in a custom UITableViewCell
虽然我发现了类似的问题,但我无法理解它的答案。
我们如何读取 UISwitch
或 UITableViewCell
中任何元素的变化?尝试使用协议,但自定义单元 class 抱怨没有初始化程序。用过,一个delegate,好像不符合view controller
protocol SwitchTableViewCellDelegate {
func didChangeSwitchValue(value: Bool)
}
class SwitchTableViewCell: UITableViewCell {
var delegate: SwitchTableViewCellDelegate
var value: Bool = true
@IBOutlet weak var switchCellLabel: UILabel!
@IBOutlet weak var switchCellSwitch: UISwitch!
@IBAction func changedSwitchValue(sender: UISwitch) {
self.value = sender.on
delegate.didChangeSwitchValue(value)
}
在cellForRowAtIndexPath
,
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
cell.delegate = self
cell.switchCellLabel?.text = "Show Cloud Music"
cell.switchCellSwitch.on = userDefaults.boolForKey(cloudMusicKey)
关于如何实施这个有什么建议吗?
我建议为此使用 Swift 闭包。在您的单元格 class:
中使用以下代码
class SwitchTableViewCell: UITableViewCell {
var callback: ((switch: UISwitch) -> Void)?
var value: Bool = true
@IBOutlet weak var switchCellLabel: UILabel!
@IBOutlet weak var switchCellSwitch: UISwitch!
@IBAction func changedSwitchValue(sender: UISwitch) {
self.value = sender.on
callback?(switch: sender)
}
然后在您的 cellForRowAtIndexPath
中输入此代码:
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
cell.callback = { (switch) -> Void in
// DO stuff here.
}
cell.switchCellLabel?.text = "Show Cloud Music"
cell.switchCellSwitch.on = userDefaults.boolForKey(cloudMusicKey)
首先,由于可以有多个单元共享同一个委托,委托应该知道哪个单元调用它。因此,您的协议方法应该提供单元格本身,而不仅仅是它的开关值。其实我们可以省略switch value参数,因为它可以从cell中查询到。
protocol SwitchTableViewCellDelegate {
func switchTableViewCellDidChangeSwitchValue(cell: SwitchTableViewCell)
}
在您的委托的协议方法实现中,您可以像这样访问开关值:
func switchTableViewCellDidChangeSwitchValue(cell: SwitchTableViewCell) {
let switchValue = cell.value
}
其次,委托 属性 可以是 nil 所以它的类型必须是 Optional
.
var delegate: SwitchTableViewCellDelegate?
要在 value
更改时调用委托:
delegate?.switchTableViewCellDidChangeSwitchValue(self)
虽然我发现了类似的问题,但我无法理解它的答案。
我们如何读取 UISwitch
或 UITableViewCell
中任何元素的变化?尝试使用协议,但自定义单元 class 抱怨没有初始化程序。用过,一个delegate,好像不符合view controller
protocol SwitchTableViewCellDelegate {
func didChangeSwitchValue(value: Bool)
}
class SwitchTableViewCell: UITableViewCell {
var delegate: SwitchTableViewCellDelegate
var value: Bool = true
@IBOutlet weak var switchCellLabel: UILabel!
@IBOutlet weak var switchCellSwitch: UISwitch!
@IBAction func changedSwitchValue(sender: UISwitch) {
self.value = sender.on
delegate.didChangeSwitchValue(value)
}
在cellForRowAtIndexPath
,
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
cell.delegate = self
cell.switchCellLabel?.text = "Show Cloud Music"
cell.switchCellSwitch.on = userDefaults.boolForKey(cloudMusicKey)
关于如何实施这个有什么建议吗?
我建议为此使用 Swift 闭包。在您的单元格 class:
中使用以下代码class SwitchTableViewCell: UITableViewCell {
var callback: ((switch: UISwitch) -> Void)?
var value: Bool = true
@IBOutlet weak var switchCellLabel: UILabel!
@IBOutlet weak var switchCellSwitch: UISwitch!
@IBAction func changedSwitchValue(sender: UISwitch) {
self.value = sender.on
callback?(switch: sender)
}
然后在您的 cellForRowAtIndexPath
中输入此代码:
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
cell.callback = { (switch) -> Void in
// DO stuff here.
}
cell.switchCellLabel?.text = "Show Cloud Music"
cell.switchCellSwitch.on = userDefaults.boolForKey(cloudMusicKey)
首先,由于可以有多个单元共享同一个委托,委托应该知道哪个单元调用它。因此,您的协议方法应该提供单元格本身,而不仅仅是它的开关值。其实我们可以省略switch value参数,因为它可以从cell中查询到。
protocol SwitchTableViewCellDelegate {
func switchTableViewCellDidChangeSwitchValue(cell: SwitchTableViewCell)
}
在您的委托的协议方法实现中,您可以像这样访问开关值:
func switchTableViewCellDidChangeSwitchValue(cell: SwitchTableViewCell) {
let switchValue = cell.value
}
其次,委托 属性 可以是 nil 所以它的类型必须是 Optional
.
var delegate: SwitchTableViewCellDelegate?
要在 value
更改时调用委托:
delegate?.switchTableViewCellDidChangeSwitchValue(self)