简体   繁体   English

关于 RxSwift/RxCocoa 与 combineLatest 绑定的问题

[英]Question about RxSwift/RxCocoa binding with combineLatest

Below is a simple example of binding a string array of viewModel to UITableView.下面是一个简单的例子,将 viewModel 的字符串数组绑定到 UITableView。

I want to subscribe to one more viewModel.randomString and use it in the cell.我想再订阅一个 viewModel.randomString 并在单元格中使用它。
I tried using combineLatest as below, but of course I couldn't bind to tableview.我尝试如下使用 combineLatest,但当然我无法绑定到 tableview。
Do you have any ideas on how to implement it?您对如何实施它有任何想法吗?

class SimpleViewModel {
    var list = BehaviorRelay<[String]>(value: [])
    var randomString = BehaviorRelay<String>(value: "")
    
    func fetch() {
        // Request...
        list.accept(["result1", "result2", "result3"])
        randomString.accept("Random...")
    }
}

class SimpleViewController {
    let tableView = UITableView()
    let viewModel = ViewModel()
    
    func foo() {
        // It works well.
        viewModel.list.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { (index, element, cell) in
            cell.textLabel?.text = element
        }
        
        // I want to bind the viewModel.list to the tableView and use the viewModel.randomString string together.
        Observable.combineLatest(viewModel.list, viewModel.randomString)
        // How???
//            .bind(to: tableView.rx.items(cellIdentifier: "Cell")) { (index, element, cell) in
//                cell.textLabel?.text = element + "RandomString" // I want to use the "RandomString" of viewModel.randomString
//            }
    }
}

Your combineLatest call produces a Observable<([String], String)> , but in order to bind to the table view items, you need an observable of a Sequence of things.您的combineLatest调用产生一个Observable<([String], String)> ,但为了绑定到表视图项,您需要一个可观察的事物Sequence

([String], String) is not a sequence. ([String], String)不是一个序列。 It is a pair of things.这是一对东西。 You need to find a way to convert that to a sequence of things.您需要找到一种方法将其转换为一系列事物。 Since you want the same randomString for each cell, you can use a function like this:由于您希望每个单元格都使用相同的randomString ,因此您可以像这样使用 function:

{ (list, randomString) in list.map { (element: $0, randomString: randomString) } }

to convert that to a [(String, String)] , with the second string in each pair in the array being the randomString .将其转换为[(String, String)] ,数组中每对中的第二个字符串是randomString

If you just pass the above function to Observable.map , you can convert a Observable<([String], String)> to Observable<[(String, String)]> :如果您只是将上述 function 传递给Observable.map ,则可以将Observable<([String], String)>转换为Observable<[(String, String)]>

Observable.combineLatest(viewModel.list, viewModel.randomString)
    .map { (list, randomString) in list.map { (element: $0, randomString: randomString) } }
    .bind(to: tableView.rx.items(cellIdentifier: "cell")) {
        (index, model, cell) in
        cell.textLabel?.text = model.element + model.randomString
    }.disposed(by: disposeBag)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM