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

时间:2018-05-23 05:08:58

标签: ios swift generics rx-swift

我有一个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)

1 个答案:

答案 0 :(得分: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")
  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. 提供单元格工厂闭包,换句话说,将闭包中的单元格出列并将其返回:

    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)
    
  3. 两者都有利弊。第二个引用tableView,有时可以非常方便。