我有一个tableView,它有两个cells:StaticSupportTableViewCell和SupportTableViewCell。顾名思义,第一个cell是tableView顶部的单个静态单元。SupportTableViewCell可以是任意数字,应该显示在静态cell下面。
我有binds的代码,并返回正确的cell
viewModel.multiContent.bind(to: tableView.rx.items) { tableView, index, item in
if let cellViewModel = item as? StaticSupportTableViewCellViewModel {
let cell = tableView.dequeueReusableCell(withIdentifier: StaticSupportTableViewCell.identifier) as? StaticSupportTableViewCell
cell?.viewModel = cellViewModel
guard let guardedCell = cell else { return UITableViewCell()}
return guardedCell
}
if let cellViewModel = item as? SupportTableViewCellViewModel {
let cell = tableView.dequeueReusableCell(withIdentifier: SupportTableViewCell.identifier) as? SupportTableViewCell
cell?.viewModel = cellViewModel
guard let guardedCell = cell else { return UITableViewCell()}
return guardedCell
}
else { return UITableViewCell() }
}.disposed(by: disposeBag)在viewModel中,我有multiContent变量:
var multiContent = BehaviorRelay<[Any]>(value: [])现在,如果我将cell viewModels一个接一个地接收到该中继上,它就会工作:
这工作:multiContent.accept([StaticSupportTableViewCellViewModel(myString: "TESTING")])
或者这样做:multiContent.accept(mainService.serviceProviders.compactMap { SupportTableViewCellViewModel(serviceProvider: $0, emailRelay: emailRelay)})
但如果我同时尝试.
multiContent.accept([StaticSupportTableViewCellViewModel(myString: "TESTING")])
multiContent.accept(mainService.serviceProviders.compactMap { SupportTableViewCellViewModel(serviceProvider: $0, emailRelay: emailRelay)})...only显示了最后一个单元格。这就像最后一个取代了第一个,而不是作为它的一个补充。
那么,如何将两个cell viewModels都接受到中继,以便两者都显示在tableView中?
编辑--我把两个cell viewModels添加到一个array中,这样做是正确的。
let contents: [Any] = [StaticSupportTableViewCellViewModel(brand: name, url: web.localized(), phoneNumber: phone.localized()), mainService.serviceProviders.compactMap { SupportTableViewCellViewModel(serviceProvider: $0, emailRelay: emailRelay)}]并更改了binding
if let cellViewModels = item as? [SupportTableViewCellViewModel] {...但是,这是有问题的,因为我一直在使用和array of [SupportTableViewCellViewModel]。当它们相互覆盖时,它不能循环它们并返回cells。
解决方案是发送cell viewModel SupportTableViewCellViewModel而不是[SupportTableViewCellViewModel],但是如何做到这一点呢?
发布于 2020-09-01 11:57:56
BehaviorRelay只发出最近接受的数组。当您调用accept(_:)时,您不是在添加中继的状态,而是要替换它的状态。要同时发出这两个数组,您需要连接这两个数组。
通常,您应该避免在生产代码中使用中继和主题。它们有利于学习Rx系统和示例,但在实际情况下很难得到正确的结果。只有在将非Rx代码转换为Rx或设置反馈循环时才需要它们。
此外,一个主题,继电器或可观察不应该是一个var。一旦安装完毕,您就不想替换一个。代之以let。
https://stackoverflow.com/questions/63684007
复制相似问题