【发布时间】:2016-03-20 20:07:01
【问题描述】:
我有以下协议和 ViewModel
class ViewModel1 {}
class ViewModel2 {}
protocol CellViewModelType {
typealias ViewModel
func bind(vm:ViewModel)
}
class TVC1 : UITableViewCell, CellViewModelType {
typealias ViewModel = ViewModel1
func bind(vm:ViewModel)
}
class TVC2 : UITableViewCell, CellViewModelType {
typealias ViewModel = ViewModel2
func bind(vm:ViewModel)
}
此设置允许我将 ViewModel 绑定到 TableViewCell 类。 但我苦苦挣扎的地方是绑定本身:
// error: Protocol 'CellViewModelType' can only be used as a generic constraint because it has Self or associated type requirements
let cell = tableView.dequeueReusableCellWithIdentifier(item.0, forIndexPath: indexPath) as! CellViewModelType
cell.bind(someViewModelReference)
到目前为止我想出的唯一解决方法是使用动态调度 (performSelector),我想知道是否有办法直接调用该方法而不使用运行时。
【问题讨论】:
标签: ios swift uitableview protocols associated-types