[英]RxDataSource: Nest CollectionView in TableViewCell
部分定義:
enum ItemDetailTableViewItem {
case itemInfoTopItem(info: ItemDetailViewModelPlus)
case itemHintItem(hint: ItemDetailViewModelPlus)
case itemManaColdownItem(manacd: ItemDetailViewModelPlus)
case itemNotesItem(notes: ItemDetailViewModelPlus)
case itemAttribItem(attrib: ItemDetailViewModelPlus)
case itemLoreItem(lore: ItemDetailViewModelPlus)
case itemComponentsItem(components: ItemDetailViewModelPlus)
}
enum ItemDetailTableViewSection {
case infoSection(items: [ItemDetailTableViewItem])
case hintSection(items: [ItemDetailTableViewItem])
case manacdSection(items: [ItemDetailTableViewItem])
case notesSection(items: [ItemDetailTableViewItem])
case attribSection(items: [ItemDetailTableViewItem])
case loreSection(items: [ItemDetailTableViewItem])
case componentsSection(items: [ItemDetailTableViewItem])
}
extension ItemDetailTableViewSection: SectionModelType {
typealias Item = ItemDetailTableViewItem
var items: [ItemDetailTableViewItem] {
switch self {
case .infoSection(items: let items):
return items
case .hintSection(items: let items):
return items
case .manacdSection(items: let items):
return items
case .notesSection(items: let items):
return items
case .attribSection(items: let items):
return items
case .loreSection(items: let items):
return items
case .componentsSection(items: let items):
return items
}
}
init(original: ItemDetailTableViewSection, items: [Self.Item]) {
self = original
}
}
我有這樣的數據源:
struct ItemDetailDataSource {
typealias DataSource = RxTableViewSectionedReloadDataSource
static func dataSource() -> DataSource<ItemDetailTableViewSection> {
return .init { (dataSource, tableView, indexPath, item) -> UITableViewCell in
switch dataSource[indexPath] {
case .itemInfoTopItem(let info):
case .itemHintItem(let hint):
case .itemManaColdownItem(let manacd):
case .itemNotesItem(let notes):
case.itemAttribItem(let attrib):
case .itemLoreItem(let lore):
case .itemComponentsItem(let components):
guard let cell = tableView
.dequeueReusableCell(withIdentifier: ConstantsForCell.itemComponentTableViewCell,
for: indexPath)
as? ItemComponentTableViewCell else {
return UITableViewCell()
}
cell.registerCell()
cell.configure(components)
return cell
}
}
}
}
在組件案例中,我有 1 個單元格,在此單元格中我創建了 CollectionView:
class ItemComponentTableViewCell: UITableViewCell {
@IBOutlet weak var itemComponentCollectionView: UICollectionView!
var components = BehaviorSubject<[String]>(value: [])
let disposeBag = DisposeBag()
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func registerCell() {
itemComponentCollectionView.register(UINib(nibName: "ComponentCollectionViewCell",
bundle: nil),
forCellWithReuseIdentifier: "ComponentCollectionViewCell")
}
func configure(_ viewModel: ItemDetailViewModelPlus) {
components.onNext(viewModel.components)
components
.bind(to: itemComponentCollectionView
.rx
.items(cellIdentifier: "ComponentCollectionViewCell",
cellType: ComponentCollectionViewCell.self)) { _, element, cell in
cell.configure(element)
}
.disposed(by: disposeBag)
}
}
我收到了這樣的錯誤:“斷言失敗:這是一項警告您之前已經在某處設置了委托(或數據源)的功能。您嘗試執行的操作將清除該委托(數據源)和意味着您的某些依賴於該委托(數據源)設置的功能可能會停止工作。”
我試圖通過設置 collectionView datasource = nil 和 delegate = nil 來修復它,但它再次出現錯誤“代理從第一次設置時更改。”
誰能幫我? 謝謝大家。 對不起,我的英語不好
細胞得到重用。 在綁定新的使用之前,您需要解除綁定之前使用的單元格。 這可以做到:
class ItemComponentTableViewCell: UITableViewCell {
@IBOutlet weak var itemComponentCollectionView: UICollectionView!
var components = BehaviorSubject<[String]>(value: [])
var disposeBag = DisposeBag()
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = DisposeBag()
}
// other methods as necessary.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.