0

So i am trying implementing collectionView inside tableviewCell and i get a weird behaviour after fetching data.

This is the 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)
}
}

this are the prints:

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 -> subscribed 2021-11-08 19:53:57.834: -> subscribed 2021-11-08 19:53:57.835: -> Event next([]) 2021-11-08 19:53:57.835: PortfolioPieTableViewViewModelCell -> Event next([]) 2021-11-08 19:53:57.836: -> Event next([["800000-402", "800000-490", "800000-436", "800000-427", "800000-433", "800000-202", "800000-271", "800000-270"]]) 2021-11-08 19:53:57.836: PortfolioPieTableViewViewModelCell -> Event next([["800000-402", "800000-490", "800000-436", "800000-427", "800000-433", "800000-202", "800000-271", "800000-270"]])

As you can see i am getting the values yet i am not entering the drive method:

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

1 Answers1

1

This is not an RxSwift issue. The closure you pass to collectionView.rx.items is used by the OS and will only be called if the OS calls it. If that closure is not getting called when items exist, then it's because the OS doesn't need any cells to display.

This will happen for example if the collection view's size is zero. I suggest you examine your layout of this cell. I suspect you will find a problem there.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • As you mentioned the issue was in the layout ob the tableViewCell. I am refactoring a code and the last coder did heightForRowAt by identifiers... and i didnt added my new identifier there. – ironRoei Nov 09 '21 at 15:38