【发布时间】:2018-11-20 10:45:48
【问题描述】:
我有一个照片查看应用程序,我使用Alamofire 获取数据并使用RxSwift 观察API 中的变化。一切正常,我可以打印返回项目的总数,甚至打印单个项目,但是当我将我的 UIlabel 文本设置为从代码中获取的标题值时,应用程序就会崩溃。我不知道为什么这是任何帮助将不胜感激。
崩溃是Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
主单元格
func configueCell(flickerPhotoModel: FlickrPhotoModel) -> Void {
self.layer.backgroundColor = UIColor.lightGray.cgColor
print("Cell \(String(describing: flickerPhotoModel.id))")
flikrTitle.text = flickerPhotoModel.title
}
PhotoViewModel
private func setupPageBinding() {
currentState.asObservable()
.subscribe(onNext: { currentState in
if currentState == .initialLoading || currentState == .loadingNextPage {
self.fetchNextPageOfPhotos()
}
}).disposed(by: disposeBag)
photos.asObservable()
.map { $0.count > 0 ? self.currentPage.value + 1 : self.currentPage.value }
.bind(to: currentPage)
.disposed(by: disposeBag)
}
控制器
private func setupCollectionViewBinding() {
print("JSONNN x \(self.viewModel.photos.value.count)")
viewModel.photos
.asObservable()
.bind(to: collectionView.rx.items(cellIdentifier: reuseIdentifier, cellType: MainCell.self))
{ _, photo, cell in
cell.configueCell(flickerPhotoModel: photo)
print("JSONNN 5 \(self.viewModel.photos.value.count)")
}
.disposed(by: disposeBag)
viewModel.currentState
.asObservable()
.map { $0 == .initialLoading || $0 == .initialLoadingFailure }
.bind(to: collectionView.rx.isHidden)
.disposed(by: disposeBag)
collectionView.rx.modelSelected(FlickrPhotoModel.self)
.subscribe(onNext: {
self.routingDelegate?.handlePhotoSelection($0)
})
.disposed(by: disposeBag)
}
【问题讨论】:
-
单元格是否注册到集合视图?
-
我不明白你的问题@PeterPajchl
-
我在viewController中注册了这样
collectionView.register(MainCell.self, forCellWithReuseIdentifier: reuseIdentifier) -
检查你的 flikrTitle 标签和 flickerPhotoModel.title,其中一个应该是 nil。
-
print("Cell label:\(flikrTitle) flickerPhotoModel.title:\(String(describing: flickerPhotoModel.title))") 并检查哪一个是 nil。如果两者都有价值,那么你的问题就在不同的地方。
标签: ios swift alamofire rx-swift