gpt4 book ai didi

ios - 如何将数据从 ViewModel 层传递到 View 层

转载 作者:行者123 更新时间:2023-11-28 15:50:41 25 4
gpt4 key购买 nike

我不知道如何将数据从我的 viewModel 传递到我的 View 并最终在 View 中显示数据,我的 View 模型类是:

class MainViewModel {

let sessionController: SessionController
weak var mainViewCoordinator: MainViewCoordinator?
public var fakeUsers: [User]?

init(sessionController: SessionController = SessionController()) {
self.sessionController = sessionController
}

func viewDidLoad() {
Service.instance.execute(resource: User.fake) { (result) in
print("\n result \(result)\n")
self.fakeUsers = result.value
}
}
}

我正在使用 Unbox Swift JSON 解码器,打印的结果是用户对象数组,如下所示:

([
User(id: "5851ac2615801e2348e4ea07",
birthDate: "2016-09-17T07:22:09.985Z",
msisdn: "912 065 979",
email: "rosa_bravo@yahoo.com",
profileImageUrl: "https://s3.amazonaws.com/...",
repPoints: 41607,
created: "2016-12-14T20:31:34.185Z",
displayName: "Victoria Escobedo"),
User(id: "5851ac2615801e2348e4ea09",
birthDate: "2016-05-06T11:38:23.678Z",
msisdn: "958842030",
email: "francisca_barrios@gmail.com",
profileImageUrl: "https://s3.amazonaws.com/...",
repPoints: 71408,
created: "2016-12-14T20:31:34.198Z",
displayName: "Gonzalo Rascón"),
User(id: "5851ac2615801e2348e4ea08",
birthDate: "2016-05-29T18:12:32.423Z",
msisdn: "905534639",
email: "ral0@gmail.com",
profileImageUrl: "https://s3.amazonaws.com/...",
repPoints: 24164,
created: "2016-12-14T20:31:34.195Z",
displayName: "Ramiro Dueñas"),
...
])

我想将结果传递给 View 层 (MainViewController) 以在表格 View 单元格中显示每个用户:

类 MainViewController:UIViewController,UITableViewDataSource {

@IBOutlet var tableview:UITableView!
var viewModel: MainViewModel = MainViewModel()

override func viewDidLoad() {
super.viewDidLoad()
viewModel.viewDidLoad()
self.tableview?.reloadData()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Action", style: .plain, target: self, action: #selector(action))
}

func action() {
viewModel.userDidSelectItem(identifier: "xxxx")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.fakeUsers?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "fakeUserObjectCell") as! MainTableViewCell
cell.fakeUserObject = viewModel.fakeUsers?[(indexPath as NSIndexPath).row]
return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let detailMainViewController = segue.destination as! DetailMainViewController
if let indexPath = tableview.indexPath(for: sender as! MainTableViewCell) {
detailMainViewController.id = viewModel.fakeUsers?[(indexPath as NSIndexPath).row].id
}
}
}

我知道我必须实现 self.tableview?.reloadData() 来显示假用户,但我不知道如何传递数据并最终显示它。

最佳答案

viewModel.viewDidLoad() 中的以下 Service.instance.execute() 是一个异步调用。它尚未完成提取,您已调用 self.tableview?.reloadData() 因此这可能导致 0 行。

我建议将 ViewModel viewDidLoad() 更改为完成调用

func viewDidLoad() {
Service.instance.execute(resource: User.fake) { (result) in
print("\n result \(result)\n")
self.fakeUsers = result.value
}
}

像这样:

func loadUsers(completion: () -> ()) {
Service.instance.execute(resource: User.fake) { (result) in
print("\n result \(result)\n")
self.fakeUsers = result.value
completion()
}
}

因此,在 MainViewController 中,viewDidLoad,您可以将调用更改为仅在准备就绪时调用 reloadData()

override func viewDidLoad() {
super.viewDidLoad()
viewModel.loadUsers {
self.tableview?.reloadData()
}
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Action", style: .plain, target: self, action: #selector(action))
}

关于ios - 如何将数据从 ViewModel 层传递到 View 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42322646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com