我正在为我最近的项目使用VIPER架构。 但我想知道如何实现UITableView数据源。用于VIPER中UITableView数据源的ViewModel swift
在VIPER中,视图是被动的。他们将事件发送给Presenter,然后Presenter将适当的ViewData(ViewModel)发送到View。
所以我在视图中存储ViewView的ViewModels。 (View不问数据演示。)
class View: UIViewController, UITableViewDataSource {
var viewModels: [CellViewModelType] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let viewModel = viewModels[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIndetifier", for: indexPath)
cell.configure(with: viewModel)
return cell
}
}
当用户的tableView选择项目,查看与indexPath发送didSelectItemAt
事件演示。然后Presenter决定事件的正确动作,推送VC或获取数据。
所以Presenter应该知道ViewData(或Real Data)。 或Presenter将事件传递给Interactor,因为Interactor存储实数据。
这是我的同步困境。数据传播查看,演示者,交互者。他们可能会有所不同,因为真实数据可以在后台异步更改(如聊天应用程序)
任何帮助将不胜感激。 :D