我尝试创建一个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)
}
答案 0 :(得分:1)
您需要在dataSources绑定之后设置您的委托。
collectionView
.rx.delegate
.setForwardToDelegate(self, retainDelegate: false)
答案 1 :(得分:0)
尝试在viewDidLoad()
中添加以下内容collectionView.delegate = self
collectionView.dataSource = self
然后将delegate和datasouce实现为类的扩展,如下所示:
extension MyClass: UICollectionViewDelegate, UICollectionViewDataSource {
// add extension methods here such as cellForItemAt
}