【发布时间】:2020-12-20 08:27:24
【问题描述】:
我有一个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)})
...只显示最后一个单元格。就像最后一个取代了第一个,而不是添加到它。
那么如何将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 和 [SupportTableViewCellViewModel] 困住了。当它们相互覆盖时,循环它们并返回 cells 是行不通的。
解决方案是发送cell viewModel SupportTableViewCellViewModel 而不是[SupportTableViewCellViewModel],但我该怎么做呢?
【问题讨论】:
标签: ios swift uitableview tableview rx-swift