RxSwift – 无法推断通用参数’Self’
我有一个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")
-
要在
items(cellIdentifier:cellType:)
提供单元格类型items(cellIdentifier:cellType:)
,这基本上就是你在做什么:countries .bind(to: tableView.rx.items(cellIdentifier: "myCell", cellType: MyCell.self)) { (row, element, cell) in // configure cell } .disposed(by: disposeBag)
-
为了提供单元工厂闭包,换句话说,将单元格出现在闭包中并返回它:
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
,有时可以非常方便。