我正在尝试将数据附加到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)
但是我收到以下警告:行上的“ call中的额外参数” .bindTo
。我尝试添加一个新的单元格并获得相同的结果。不知道它是否相关,但是我已经注册了该单元格。
我在这里读到,如果参数类型不匹配,您会收到此警告:Swift-调用中的额外参数。但是,看起来参数可以很好地匹配。
我是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)
在我曾经使用过(出租车)的地方,这里有很多出租车物品。见下图:
同样,一旦我调用.asObservable(),我就会得到以下内容:
我设法删除了这些行以打印出来.bindTo
。如果我加回那条线,我会得到和以前一样的错误。
重要提示:我在之前链接的文章中使用了代码库。如果我从ChoiceCell中删除,我可以复制相同的错误:
// var choiceModel: ChoiceModel? {
// didSet {
// layoutCell()
// }
// }
Thanks to Philip Laine answer above: https://stackoverflow.com/a/36536320/2126233
That helped me see that I was making a mistake in regards to what I was observing. It helped me to see the problem I was having in my code.
If you just want to bind to a normal tableViewCell then you need to use tableView.rx_itemsWithCellFactory
:
currentQuestion.asObservable()
.bindTo(tableView.rx_itemsWithCellFactory) {(tableView, row, item) in
let cell = UITableViewCell()
cell.textLabel?.text = item.distance
return cell
}.addDisposableTo(disposeBag)
If you are using a custom cell then you can use tableView.rx_itemsWithCellIdentifier("ChoiceCell", cellType: ChoiceCell.self)
. Here is an example:
currentQuestion
.asObservable()
.filter { $0 != nil }
.map { $0!.choices }
.bindTo(tableView.rx_itemsWithCellIdentifier("ChoiceCell", cellType: ChoiceCell.self)) { (row, element, cell) in
cell.choiceModel = element
}
.addDisposableTo(disposeBag)
For me I was still getting this same error unless I had a property inside the tableView cell that matched the element that was coming out of the array I was binding the tableView to.
So if you have an array of [Taxis] like so then inside the tableViewCell I required a variable that stored a Taxis. Then I was able to compile my project.
So in ChoiceCell I have a var like so:
var taxi: Taxi? {
didSet {
layoutCell()
}
}
我希望这可以帮助其他任何将tableViewCell绑定到数组的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句