我在ViewController中有一个CollectionView,并且试图从Realm获取对象列表,并使用RxSwift将其绑定到CollectionView。
问题是我遇到了错误:
该行中的“对成员'items(cellIdentifier:celltype :)'的引用不明确”
: .bind(收件人:collection.rx.items(cellIdentifier ...)
这是代码:
import UIKit
import RxRealm
import RxSwift
import RxCocoa
class MyListViewController: UIViewController {
var myList: MyList?
private var collection: UICollectionView?
{...}
private func loadMyList() {
let myList = retrieveMyListFromDb()
guard
let list = myList,
let collection = collection
else { return }
let disposeBag = DisposeBag()
Observable.from(list)
.bind(
to: collection.rx.items(
cellIdentifier: HomeMovieCollectionViewCell.identifier,
cellType: HomeMovieCollectionViewCell.self)
) { (row, element, cell) in
}
.disposed(by: disposeBag)
}
private func retrieveMyListFromDb() -> MyList? {
return RealmManager().objects(MyList.self)?.filter {
$0.userId == 0
}.first
}
这是MyList代码:
import Foundation
import Realm
import RealmSwift
@objcMembers
class MyList: Object {
dynamic var userId: Int = 0
var movies = List<Movie>()
public override static func primaryKey() -> String? { return "userId" }
}
答案 0 :(得分:0)
感谢用户DanielT。我意识到问题是集合视图的rx.items函数需要项数组或列表,并且我将其与对象一起使用。我遇到的错误并不能完全说明正在发生的一切。
解决方案应该是
Observable.collection(from: list.movies)
.bind(to: collection.rx.items(
cellIdentifier: HomeMovieCollectionViewCell.identifier,
cellType: HomeMovieCollectionViewCell.self)
) { (row, element, cell) in
}
.disposed(by: disposeBag)