collectionView с RxSwift не показывает никаких элементов

Я пытаюсь создать collectionView и заполнить его данными с помощью RxSwift. Однако, хотя кажется, что он возвращает объекты в datasource.configureCell, он не показывает никаких ячеек. Я подозреваю, что что-то не так с моей настройкой в ​​viewDidLoad?

Настройка collectionView

    // Create a waterfall layout
    let layout = CHTCollectionViewWaterfallLayout()

    //Add CollectionView
    self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), collectionViewLayout: layout)

    self.view.addSubview(collectionView)

    //Customize
    self.collectionView!.alwaysBounceVertical = true

    // Collection view attributes
    self.collectionView.autoresizingMask = [UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleWidth]
    self.collectionView.alwaysBounceVertical = true

    //Register cell
    collectionView.register(PetsaleCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    //Constraints
    self.collectionView.snp.makeConstraints({ make in
        make.bottom.equalTo(0)
        make.left.equalTo(0)
        make.right.equalTo(0)
        make.top.equalTo(0)

    })

    //Datasource
    setUpDataSource()

setUpDataSource

func setUpDataSource() {
    dataSource.configureCell = { (_, tv, ip, animal: Animal) in

        let cell = tv.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier, for: ip) as! PetsaleCell
        cell.petCellViewModel = PetCellViewModel(animal: animal)

        return cell
    }



    let loadNextPageTrigger = self.collectionView.rx.contentOffset
        .flatMap { _ in
            self.collectionView
                .isNearBottomEdge(edgeOffset: 20.0)
                ? Observable.just(())
                : Observable.empty()
    }

    animalViewModel.rx_animals(loadNextPageTrigger)
        .asDriver(onErrorJustReturn: .empty).map { [SectionModel(model: "Animal", items: $0.animals)] }
        .drive(collectionView.rx.items(dataSource: dataSource))
        .addDisposableTo(disposeBag)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
    return CGSize(width: 200, height: 200)
}

person Peter Pik    schedule 15.01.2017    source источник
comment
Ваша реализация источника данных Rx CollectionView помогла мне, спасибо!   -  person Rici    schedule 28.03.2018


Ответы (1)


Вам нужно установить своего делегата сразу после привязки источников данных.

collectionView
    .rx.delegate
    .setForwardToDelegate(self, retainDelegate: false)
person Makaille    schedule 16.01.2017