Skip to content

Nested Collection

Anton Goncharov edited this page May 12, 2020 · 4 revisions

UITableViewCell can contain nested UICollectionView and RxDataSourcesExt let you process it in minimalistic way. To achieve it you need to create view model that implements NestedCollectionTableCellViewModel protocol

struct EmbedCollectionCellViewModel: NestedCollectionTableCellViewModel {

    typealias TableCellType = EmbedCollectionCell

    let id: String
    let nestedSections: [CollectionSectionModel]
    let rowHeight: CGFloat = 72
}

And UITableViewCell that implements CollectionContainableCell

class EmbedCollectionCell: UITableViewCell, ConfigurableCell, CollectionContainableCell {

    let collectionView = UICollectionView()

    var disposeBag = DisposeBag()

    func configure(with viewModel: EmbedCollectionCellViewModel) {}

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

For processing events in a nested collection view TableDirector has some methods similar to func cellCreated() (see Events in Cells for details).

director.rx.nestedViewModelSelected(StringCollectionCell.self, in: EmbedCollectionCell.self)
    .subscribe()
    .disposed(by: disposeBag)

director.rx.nestedCellCreated(ButtonCollectionCell.self, in: EmbedCollectionCell.self) { $0.button.rx.tap }
    .subscribe()
    .disposed(by: disposeBag)
Clone this wiki locally