【问题标题】:RxSwift - Generic parameter 'Self' could not be inferredRxSwift - 无法推断通用参数“Self”
【发布时间】:2018-11-01 23:05:50
【问题描述】:

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

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

当我尝试在 UITableView 中绑定这些国家/地区时,它显示错误 Generic parameter 'Self' could not be inferred

这是我正在做的 sn-p:

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

【问题讨论】:

    标签: ios swift generics rx-swift


    【解决方案1】:

    我建议你使用最新版本的 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)
      

    两者各有利弊。第二个引用了tableView,这有时会非常方便。

    【讨论】:

    • 谢谢@au-ris,第二个很棒,对我有用。
    • 我遇到了同样的问题。您的方法似乎无法为我解决。这就是我的绑定方式:Orders.getList(shopId, status: .active).bind(to: rxTableView.rx.items(dataSource: dataSource)),其中Orders.getList() 的类型为Observable<>dataSource 的类型为RxTableViewSectionedReloadDataSource<MyTableSection>MyTableSection 符合 Equatable。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多