RxSwift-无法推断通用参数“ Self”

Lit*_*Dev 1 generics ios swift rx-swift

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

let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]
Run Code Online (Sandbox Code Playgroud)

当我尝试在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)
Run Code Online (Sandbox Code Playgroud)

Au *_*Ris 5

我建议您使用最新版本的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")
Run Code Online (Sandbox Code Playgroud)
  1. 要在中提供单元格类型,items(cellIdentifier:cellType:)基本上就是您正在做的事情:

    countries
        .bind(to: tableView.rx.items(cellIdentifier: "myCell", cellType: MyCell.self)) { (row, element, cell) in
            // configure cell
        }
        .disposed(by: disposeBag)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为了提供单元工厂关闭,换句话说,使单元中的单元出队并返回:

    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)
    
    Run Code Online (Sandbox Code Playgroud)

两者都有优点和缺点。第二篇文章提到tableView有时可能会非常方便。