【问题标题】:UIcollectionView inside UItableViewcell using rxSwift fail使用 rxSwift 的 UItableViewcell 内的 UIcollectionView 失败
【发布时间】:2021-12-21 13:19:55
【问题描述】:

所以我尝试在 tableviewCell 中实现 collectionView,但在获取数据后我得到了一个奇怪的行为。

这是tableViewCell:

class PortfolioPieTableViewCell: PortfolioBaseCell {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var collectionView: UICollectionView!

private var disposeBag = DisposeBag()

override class var identifier: String {
    return "PortfolioPieTableViewCell"
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func prepareForReuse() {
    super.prepareForReuse()
    disposeBag = DisposeBag()
}

func config(viewModel: PortfolioPieTableViewViewModelCell) {
    collectionView.register(UINib(nibName: "PortfolioPieCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "PortfolioPieCollectionViewCell")

    viewModel.items.debug("PortfolioPieTableViewViewModelCell ????").drive(collectionView.rx.items) { collectionView, index, element in
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PortfolioPieCollectionViewCell", for: IndexPath(row: index, section: 0)) as! PortfolioPieCollectionViewCell
        cell.configureCell(element)
        return cell
            
    }.disposed(by: disposeBag)
    
    viewModel.fetchData()
}
}


 This is the viewModel:




final class PortfolioPieTableViewViewModelCell {

//MARK:- Output
private let _items = BehaviorRelay<[[String]]>(value: [])
lazy var items = _items.asDriver(onErrorJustReturn: []).debug(" ????")

private let positions: [[String]]


init(pieList: [[String]]) {
    self.positions = pieList
}

func fetchData() {
    var items = [[String]]()

    positions.forEach { position in
        items.append(position)
    }
    _items.accept(items)
}
}

这是印刷品:

021-11-08 19:53:57.830: PortfolioPieTableViewViewModelCell ???? -> isDisposed 2021-11-08 19:53:57.830:???? -> isDisposed 2021-11-08 19:53:57.833:​​ PortfolioPieTableViewViewModelCell ???? -> 订阅 2021-11-08 19:53:57.834:???? -> 订阅 2021-11-08 19:53:57.835:???? -> 事件下一个([]) 2021-11-08 19:53:57.835: PortfolioPieTableViewViewModelCell ???? -> 事件下一个([]) 2021-11-08 19:53:57.836:???? -> 事件下一个([[“800000-402”、“800000-490”、“800000-436”、“800000-427”、“800000-433”、“800000-202”、“800000-271”、“ 800000-270"]]) 2021-11-08 19:53:57.836: PortfolioPieTableViewViewModelCell ???? -> 事件下一个([[“800000-402”、“800000-490”、“800000-436”、“800000-427”、“800000-433”、“800000-202”、“800000-271”、“ 800000-270"]])

如您所见,我正在获取值但我没有输入驱动方法:

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PortfolioPieCollectionViewCell", for: IndexPath(row: index, section: 0)) as! PortfolioPieCollectionViewCell
            cell.configureCell(element)
            return cell

【问题讨论】:

    标签: ios swift uitableview uicollectionview rx-swift


    【解决方案1】:

    这不是 RxSwift 问题。您传递给collectionView.rx.items 的闭包由操作系统使用,并且只有在操作系统调用它时才会被调用。如果在项目存在时没有调用该闭包,那是因为操作系统不需要显示任何单元格。

    例如,如果集合视图的大小为零,就会发生这种情况。我建议你检查你的这个单元格的布局。我怀疑你会在那里发现问题。

    【讨论】:

    • 正如您所提到的,问题出在 tableViewCell 的布局中。我正在重构一个代码,最后一个编码人员通过标识符做了 heightForRowAt ......我没有在那里添加我的新标识符。
    猜你喜欢
    • 2018-05-18
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多