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