私はUIViewController
に次のコードを持っている:UITableViewのタップ/選択のためにRxSwiftの前と現在の値を観察する方法は?
_ = viewModel.selectedCountryId.asObservable()
.distinctUntilChanged()
.scan((0, 0), accumulator: { (prev, curr) in
return zip(prev, curr)
})
.subscribe(onNext: {
let prevCell = self.tableView.cellForRow(at: IndexPath(row: previous, section: 0))
prevCell?.accessoryType = .none
let currCell = self.tableView.cellForRow(at: IndexPath(row: curr, section: 0))
currCell?.accessoryType = .checkmark
}).disposed(by: disposeBag)`
、私はいくつかのSOの記事を調べて解決策であると信じるscan
に何をするか分かりません。
私が達成したいのは、行を選択して、前のテーブルの選択を解除して、テーブルビューに右側のチェックマークアイコン付きの単一の選択メカニズムを持たせることです。
いくつかの操作を行った後、別のビューコントローラに渡すために選択した行を保存する必要があるため、viewModelからVariable<Int>
を使用します。
何か間違いがありましたら、お勧めします。私はまだMVVM e RxSwiftの初心者です。
EDIT:
ここでは、項目が選択されているサブスクリプションです。
_ = tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in self?.viewModel.selectedCountryId.value = indexPath.row })