【发布时间】:2016-06-07 02:58:30
【问题描述】:
代码取自Protocol Oriented MVVM,这就是ViewModel 的样子:
struct MinionModeViewModel: SwitchWithTextCellDataSource {
var title = "Minion Mode!!!"
var switchOn = true
}
extension MinionModeViewModel: SwitchWithTextCellDelegate {
func onSwitchTogleOn(on: Bool) {
if on {
print("The Minions are here to stay!")
} else {
print("The Minions went out to play!")
}
}
var switchColor: UIColor {
return .yellowColor()
}
}
我理解这部分。基本上,MinionModeViewModel 覆盖了SwitchWithTextCellDelegate 和SwitchWithTextCellDelegate 的一些默认行为
接下来作者通过传递viewModel 作为参数来配置单元格:
SettingsViewController.swift
let viewModel = MinionModeViewModel()
cell.configure(withDataSource: viewModel, delegate: viewModel)
return cell
然而,在SwitchWithTextTableViewCell,configure 方法的参数是SwitchWithTextCellDataSource 和SwitchWithTextCellDelegate
func configure(withDataSource dataSource: SwitchWithTextCellDataSource, delegate: SwitchWithTextCellDelegate?) {
self.dataSource = dataSource
self.delegate = delegate
label.text = dataSource.title
switchToggle.on = dataSource.switchOn
// color option added!
switchToggle.onTintColor = delegate?.switchColor
}
我是 Swift 新手。谁能解释一下
-
SwitchWithTextTableViewCell中的configure方法的名称和含义是什么dataSource和delegate。他们是External Parameter Names。 -
为什么
configure方法的传递参数类型不同:view modelvsprotocols类型
【问题讨论】:
标签: swift struct swift2 swift-protocols