RxSwift 不将数据绑定到自定义集合视图单元格
RxSwift do not bind data to custom collection view cell
我创建了自定义 collectionViewCell 以将数据从可观察到自定义 CollectionViewCell 绑定。我成功地将数据绑定到自定义 TableViewCell,但无法将数据内容显示到自定义集合视图单元格。 Rx 与自定义集合视图数据源的绑定是否存在问题?
这是我的自定义集合视图单元格:
class MovieItemCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
func bind(_ viewModel: MovieItemViewModel) {
debugPrint("bind")
titleLabel.text = viewModel.title
}
}
下面是我的绑定方式:(以下代码完全适用于 tableViewCell,但不适用于 collectionViewCell。顺便说一下,调试器不会进入 collectionViewCell 中的绑定方法)
output.movies.drive(
topRatedMoviesCollectionView
.rx.items(cellIdentifier: MovieItemCollectionViewCell.reuseID,
cellType: MovieItemCollectionViewCell.self)) {_, viewModel, cell in
cell.bind(viewModel)
}.disposed(by: disposeBag)
您发布的代码没有问题,与您遇到的问题无关。
如果 "the debugger do not enter to bind methods in collectionViewCell" 那么您的 movies
observable 可能从未发出任何值。
我创建了自定义 collectionViewCell 以将数据从可观察到自定义 CollectionViewCell 绑定。我成功地将数据绑定到自定义 TableViewCell,但无法将数据内容显示到自定义集合视图单元格。 Rx 与自定义集合视图数据源的绑定是否存在问题?
这是我的自定义集合视图单元格:
class MovieItemCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
func bind(_ viewModel: MovieItemViewModel) {
debugPrint("bind")
titleLabel.text = viewModel.title
}
}
下面是我的绑定方式:(以下代码完全适用于 tableViewCell,但不适用于 collectionViewCell。顺便说一下,调试器不会进入 collectionViewCell 中的绑定方法)
output.movies.drive(
topRatedMoviesCollectionView
.rx.items(cellIdentifier: MovieItemCollectionViewCell.reuseID,
cellType: MovieItemCollectionViewCell.self)) {_, viewModel, cell in
cell.bind(viewModel)
}.disposed(by: disposeBag)
您发布的代码没有问题,与您遇到的问题无关。
如果 "the debugger do not enter to bind methods in collectionViewCell" 那么您的 movies
observable 可能从未发出任何值。