首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RxDataSource:在TableViewCell中嵌套CollectionView

RxDataSource:在TableViewCell中嵌套CollectionView
EN

Stack Overflow用户
提问于 2021-11-01 09:28:22
回答 1查看 52关注 0票数 1

截面的定义:

代码语言:javascript
运行
复制
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
    }
}

我的DataSource是这样的:

代码语言:javascript
运行
复制
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
            }
        }
    }
}

在组件的情况下,我有一个单元格,在这个单元格中我创建了CollectionView:

代码语言:javascript
运行
复制
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数据源= nil和委托= nil来修复它,但它再次出现错误"Proxy changed from the time it first set“。

有谁可以帮我?谢谢大家。对不起,我的英语不好

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-01 12:33:58

细胞得到重用。在绑定新的使用之前,您需要解除对该单元的先前使用的绑定。这是可以做到的:

代码语言:javascript
运行
复制
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.
}   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69794963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档