I have a UITableViewCell
that contain a UISwitch
. This cell has its own SwitchCellViewModel
. Lets say it contains some Bool
value (enabled vs disabled). And ViewController
is the one who contains UITableView
, creates viewModel
for the cell and sets cell with it.
I want to a achieve:
- On a cell level: change UISwitch state whenever a viewModel’s bool property value changes (without reloading tableView of course).
- On a ViewController level: handle UISwitch state change (by user).
The use case is next: cell shows some option that can be disabled or enabled. That action goes to backend, and after I receive response with a result (enabled vs disabled on backend), I have to sync view’s state again with the updated data.
I understand how to subscribe to a property value change on a cell level, so when I change it in viewModel from viewController, it updates a cell view right away. But I’m not sure how to deal with back action from UISwitch
to viewController.
Is it achievable with a single @Published
bool property in viewModel, or I have to have 2 separate things for this bidirectional case.
It looks really silly to me that I have to expose a separate Publisher
for this purpose, since I already have a @Published
property in viewmodel, that view controller should be notified about, so why wouldn’t I use it. But if I use just one, then it will be the case that ViewController sets @Published
var in viewModel, cell itself will handle it and adjust UI, but ViewController immediately gets event about it as well, since it is subscribed to on it.