我使用RxSwift使用以下代码:
self.photos
.bind(to: collectionView.rx.items(dataSource: self.dataSource))
.disposed(by: disposeBag)
它给了我Type of expression is ambiguous without more context
还需要什么上下文?
完整的代码如下所示:
//
// PhotosCollectionViewController.swift
// TodoListRxSwift
//
//
import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
struct Photo {
var name :String
var imageURL :String
}
struct SectionOfPhoto {
var header: String
var items: [Photo]
}
extension SectionOfPhoto: SectionModelType {
init(original: SectionOfPhoto, items: [Photo]) {
self = original
self.items = items
}
}
class PhotosCollectionViewController :UICollectionViewController {
private let disposeBag = DisposeBag()
private (set) var photos = BehaviorRelay(value: [Photo(name: "Pic 1", imageURL: "1.png"),Photo(name: "Pic 2", imageURL: "2.png"),Photo(name: "Pic 3", imageURL: "3.png")])
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionOfPhoto>(configureCell: { ds, cv, indexPath, photo in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath)
return cell
})
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.delegate = nil
self.collectionView?.dataSource = nil
configureObservables()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
prepareSegueForAddPhotoViewController(segue :segue)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "PhotosHeaderView", for: indexPath)
return headerView
default:
return UICollectionReusableView()
}
}
private func prepareSegueForAddPhotoViewController(segue :UIStoryboardSegue) {
guard let nc = segue.destination as? UINavigationController else {
fatalError("NavigationController does not exist")
}
guard let addPhotoVC = nc.topViewController as? AddPhotoViewController else {
fatalError("AddPhotoViewController does not exist")
}
_ = addPhotoVC.selectedPhoto.subscribe(onNext: { (photo) in
self.photos.accept(self.photos.value + [photo])
})
}
private func configureObservables() {
if let collectionView = self.collectionView {
self.photos.bind(to: collectionView.rx.items(dataSource: self.dataSource))
self.photos.bind(to: collectionView.rx.items(cellIdentifier: "PhotoCollectionViewCell", cellType: PhotoCollectionViewCell.self)) { row, model, cell in
cell.photoImageView.image = UIImage(named: model.imageURL)
}.disposed(by: disposeBag)
}
}
}
答案 0 :(得分:1)
问题是photos
是错误的类型。数据源期望元素[SectionOfPhoto]
,但是photos
具有元素[Photo]
。
但是,更改/固定照片类型会破坏addPhotoVC.selectedPhoto
,因为它试图将单个照片添加到部分区域中。