繁体   English   中英

RxDataSource:在 TableViewCell 中嵌套 CollectionView

[英]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.

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