作者:倒退淂磁带_628 | 来源:互联网 | 2023-05-17 18:08
ImtryingtocreateacollectionViewandpopulatedataintoitusingRxSwift.Howevereventhoughi
I'm trying to create a collectionView and populate data into it using RxSwift. However even though it seem to return objects in datasource.configureCell
it doesn't show any cells. I suspect that there is something wrong with my setup in viewDidLoad
?
我正在尝试创建一个collectionView并使用RxSwift将数据填充到其中。然而,即使它似乎返回数据源中的对象。它不显示任何单元格。我怀疑我在viewDidLoad的设置有问题?
Setup collectionView
设置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
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)
}
2 个解决方案