RxDataSources with UICollection

2.1k views Asked by At

I'm learning RxSwift, i want used RxDataSources with UICollectionView, but nothing is shown, please tell me where i was wrong, Thanks; My code:

---- Model ----

struct Traveler {
    let name: String
    let avater: UIImage?
}

struct Travelers {
    var travelers: [Traveler]
}

extension Travelers: SectionModelType {
    typealias Item = Traveler

    var items: [Traveler] { return travelers }

    init(original: Travelers, items: [Traveler]) {
        self = original
        travelers = items
    }
}

---- data -----

let items = Variable<[Travelers]>([
            Travelers(travelers: [Traveler(name: "Bob", avater: UIImage(named: "pic1"))])
        ])

---- View -----

let disposeBag = DisposeBag()

let dataSources = RxCollectionViewSectionedReloadDataSource<Travelers>()

let viewModel = TravelerViewModel()

lazy var travelerCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.scrollDirection = .horizontal
    layout.itemSize = CGSize(width: 44, height: 44)

    let travelerCollectionView = UICollectionView(frame: CGRect(x: 100, y: 100, width: 100, height: 100), collectionViewLayout: layout)
    travelerCollectionView.backgroundColor = .red
    travelerCollectionView.register(TravelerItemCell.self, forCellWithReuseIdentifier: TravelerItemCell.identifier)
    return travelerCollectionView
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(travelerCollectionView)

    viewModel.items.asObservable()
        .bind(to: travelerCollectionView.rx.items(dataSource: dataSources))
        .addDisposableTo(disposeBag)

    dataSources.configureCell = { (_, collection, indexPath, traveler) in
        let cell = collection.dequeueReusableCell(withReuseIdentifier: TravelerItemCell.identifier, for: indexPath) as! TravelerItemCell
        cell.setAvaterImage(avater: traveler.avater)
        return cell
    }
}

thanks, i want used UICollectionView show some people avater.

0

There are 0 answers