RxSwift - 无法推断通用参数 'Self'

RxSwift - Generic parameter 'Self' could not be inferred

我有一个 UITableView 和一个 countries 变量,其签名如下:

let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]

当我试图在 UITableView 中绑定这个国家数组时,它显示错误 Generic parameter 'Self' could not be inferred

这是我正在做的片段:

let countries = Observable.just(countryArray)
    countries.bindTo(self.tableView.rx.items(cellIdentifier: "myCell",
                                        cellType: MyCell.self)) {
                                            row, country, cell in
                                            // configuring cell
    }
    .addDisposableTo(disposeBag)

我建议您使用最新版本的 RxSwift。您现在使用的内容已弃用。你的错误可能与它有关。

有两种方法可以完成您正在做的事情:

let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]
let countries = Observable.of(countryArray)

// Be sure to register the cell
tableView.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "myCell")
  1. 要在 items(cellIdentifier:cellType:) 中提供单元格类型,这基本上就是您正在做的事情:

    countries
        .bind(to: tableView.rx.items(cellIdentifier: "myCell", cellType: MyCell.self)) { (row, element, cell) in
            // configure cell
        }
        .disposed(by: disposeBag)
    
  2. 提供单元工厂闭包,换句话说,使闭包中的单元出列并return它:

    countries
        .bind(to: tableView.rx.items) { (tableView, row, element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: IndexPath(row: row, section: 0)) as! MyCell
            // configure cell
            return cell
        }
        .disposed(by: disposeBag)
    

两者各有利弊。第二个有对 tableView 的引用,它有时非常方便。