我有一个 UITableView 和一个 countries
变量,其签名如下:
let countryArray = ["Bangladesh", "India", "Pakistan", "Nepal", "Bhutan", "China", "Malaysia", "Myanmar", "Sri Lanka", "Saudi Arabia"]
当我试图在 UITableView 中绑定(bind)这个国家数组时,它显示错误 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:)
中提供单元格类型,这基本上就是您正在做的事情: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
的引用,这有时非常方便。
关于ios - RxSwift - 无法推断通用参数 'Self',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51524767/