I'm trying to gradually convert my App to RxSwift / MVVM. But I think I'm doing some things incorrectly.
In this example I have a static table with this specific information.
let itens = Observable.just([
MenuItem(name: GlobalStrings.menuItemHome, nameClass: "GPMainVC"),
MenuItem(name: GlobalStrings.menuItemProfile, nameClass: "GPMainVC"),
MenuItem(name: GlobalStrings.menuItemLevels, nameClass: "GPLevelsVC"),
])
I need to know the model(MenuItem) and the index when the user select a cell, but I am having trouble doing that
tableView.rx
.itemSelected
.map { [weak self] indexPath in
return (indexPath, self?.modelView.itens.elementAt(indexPath.row))
}
.subscribe(onNext: { [weak self] indexPath, model in
self?.tableView.reloadData()
//can´t get MenuItem because model its a observable
//self?.didSelect((indexPath as NSIndexPath).row, name.nameClass)
})
.addDisposableTo(disposeBag)
Thanks in advance
You have to do next steps:
Use Variable. I think it's a better solution in your situation.
let itens = Variable([
MenuItem(name: GlobalStrings.menuItemHome, nameClass: "GPMainVC"),
MenuItem(name: GlobalStrings.menuItemProfile, nameClass: "GPMainVC"),
MenuItem(name: GlobalStrings.menuItemLevels, nameClass: "GPLevelsVC"),
])
Use the following code if you want to get index and model from a clicked cell.
tableView.rx
.itemSelected
.map { index in
return (index, self.items.value[index.row])
}
.subscribe(onNext: { [weak self] index, model in
// model is MenuItem class
})
.addDisposableTo(disposeBag)
I hope my answer was very helpful for you. Please let me know if you want more information about RxSwift opportunities in your task. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With