swift - RxCocoa 调用中的额外参数

标签 swift uitableview rx-swift reactivex moya

我正在尝试将数据附加到 UITableView。我已经在这里下载了项目表格,并且正在使用将数据附加到 tableView 的代码:http://yannickloriot.com/2016/01/make-uitableview-reactive-with-rxswift/ :

首先我创建了以下变量:

let currentQuestion: Variable<Taxi?>   = Variable(nil)

然后我尝试执行以下操作:

 currentQuestion
        .asObservable()
        .bindTo(tableView.rx_itemsWithCellIdentifier("ChoiceCell", cellType: ChoiceCell.self)) { (row, element, cell) in
            cell.choiceModel = element
        }
        .addDisposableTo(disposeBag)

但我收到以下警告:.bindTo 行上的“Extra argument in call”。我尝试添加一个新单元格并得到相同的结果。不确定它是否相关,但我已经注册了单元格。

我在这里读到,如果参数类型不匹配,您会收到此警告:Swift - Extra Argument in call .然而,看起来参数匹配得很好。

我是 Rx 的新手,希望有人能帮助我理解这里可能出了什么问题。谢谢。

======

编辑

这是我的新代码。我单独尝试了 rx_itemsWithCellIdentifier("ChoiceCell")rx_itemsWithCellIdentifier("ChoiceCell", cellType: ChoiceCell.self):

let currentQuestion = Variable<[Taxi]>(taxis)

    currentQuestion.asObservable()
        .bindTo(tableView.rx_itemsWithCellIdentifier("ChoiceCell")) {(row, element, cell) in
        cell.choiceModel = element
        }.addDisposableTo(disposeBag)

我用过(taxis)的地方,是taxi项目的array。见下图:

enter image description here

此外,一旦我调用了 .asObservable(),我就会得到以下信息:

enter image description here

我设法通过删除行 .bindTo 来打印这些。如果我把那行加回去,我会得到和以前一样的错误。

重要提示:我使用了我之前链接到的文章中的代码库。如果我从 ChoiceCell 中删除,我可以复制相同的错误:

//  var choiceModel: ChoiceModel? {
//    didSet {
//          layoutCell()
//    }
//  }

最佳答案

根据经验,当您尝试使用错误的预期数据类型绑定(bind)变量时,通常会在调用消息中提供额外的参数。第一个问题是您正在尝试将 Taxi 的单个实例绑定(bind)到需要一系列可观察值的 TableView 。

/**
Binds sequences of elements to table view rows.

- parameter cellIdentifier: Identifier used to dequeue cells.
- parameter source: Observable sequence of items.
- parameter configureCell: Transform between sequence elements and view cells.
- parameter cellType: Type of table view cell.
- returns: Disposable object that can be used to unbind.
*/
public func rx_itemsWithCellIdentifier<S : SequenceType, Cell : UITableViewCell, O : ObservableType where O.E == S>(cellIdentifier: String, cellType: Cell.Type = default) -> (source: O) -> (configureCell: (Int, S.Generator.Element, Cell) -> Void) -> Disposable

问题似乎不是由可选对象引起的,但我不明白您为什么要将可选对象绑定(bind)到 tableview,所以我建议您也避免这种情况。这是一个可行的示例。

let currentQuestion = Variable<[Taxi]>([Taxi()])
currentQuestion.asObservable().bindTo(tableView.rx_itemsWithCellIdentifier("ChoiceCell")) {(row, element, cell) in
  cell.choiceModel = element
}.addDisposableTo(disposeBag)

关于swift - RxCocoa 调用中的额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36536059/

相关文章:

swift - fatal error : Index out of range - Swift 4 and Firebase

Swift,为什么这个本地变量没有被释放?

ios - 如何使用 rxswift 切换 editButtonItem?

ios - 二元运算符 '<' 不能应用于类型 'Double?' 和 'Double' 的操作数

objective-c - iOS8(自定义键盘)如何使背景透明?

ios - 如何在处理 catchError 后恢复数据源

ios - 当我滚动时,TableView 仅加载一个单元格的内容

swift - RxSwift 通过 flatMaps 链传播一个值

ios - CMTimeMakeWithSeconds 中的首选时间刻度

ios - 修复了 UITableViewCells 中 ImageViews 的填充?