RxSwift - Общий параметр 'Self' не может быть выведен - PullRequest
0 голосов
/ 23 мая 2018

У меня есть 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 голосов
/ 23 мая 2018

Я бы предложил вам использовать последнюю версию 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, что иногда может быть очень удобно.

...