简体   繁体   English

RxDataSource:在 TableViewCell 中嵌套 CollectionView

[英]RxDataSource: Nest CollectionView in TableViewCell

Definition for Section:部分定义:

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
    }
}

I have DataSource like this:我有这样的数据源:

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
            }
        }
    }
}

At components case I have 1 cell, in this cell I create CollectionView:在组件案例中,我有 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)
    }
}

I received an error like this: "Assertion failed: This is a feature to warn you that there is already a delegate (or data source) set somewhere previously. The action you are trying to perform will clear that delegate (data source) and that means that some of your features that depend on that delegate (data source) being set will likely stop working."我收到了这样的错误:“断言失败:这是一项警告您之前已经在某处设置了委托(或数据源)的功能。您尝试执行的操作将清除该委托(数据源)和意味着您的某些依赖于该委托(数据源)设置的功能可能会停止工作。”

I tried to fix it by setting for collectionView datasource = nil and delegate = nil but it has an error again "Proxy changed from the time it was first set."我试图通过设置 collectionView datasource = nil 和 delegate = nil 来修复它,但它再次出现错误“代理从第一次设置时更改。”

Can anyone help me?谁能帮我? Thank all.谢谢大家。 Sorry for my bad English对不起,我的英语不好

Cells get reused.细胞得到重用。 You need to unbind the previous use of the cell before binding the new use.在绑定新的使用之前,您需要解除绑定之前使用的单元格。 This can do it:这可以做到:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM