我使用 RxDataSources 时有一个奇怪的行为。我正在使用
RxCollectionViewSectionedAnimatedDataSource
。我在集合视图中的每个单元格内都有一个按钮,它显示了一些内容。该内容通过库提供的内置动画显示。当列表的第一个单元格未完全显示时,这意味着我滚动了一点并且仅显示了单元格的一部分,如果我点击按钮(位于单元格底部),则最后一个可见单元格会显示屏幕消失并完全重新出现一秒钟。我相信这可能是由于库中的内置动画造成的,但是,它看起来太像一个错误了。
这就是我的实施方式。数据来源:
let dataSource = RxCollectionViewSectionedAnimatedDataSource<OfferSection>(
animationConfiguration: AnimationConfiguration(insertAnimation: .none, reloadAnimation: .none, deleteAnimation: .none), configureCell: {
[unowned self] dataSource, collectionView, indexPath, element in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OfferCollectionViewCell.identifier, for: indexPath) as! OffersCollectionViewCell
cell.set(offer: element)
cell.findOfferButton.rx.tap.map{indexPath}.compactMap{$0}.map{ index in
(element, index.row)}.bind(to: self.viewModel.input.viewOffer).disposed(by: cell.disposeBag)
cell.redeemButton.rx.tap.map{element}.bind(to: self.viewModel.input.redeemOffer).disposed(by: cell.disposeBag)
return cell
})
viewModel.output.offers.map{ [OfferSection(header: "", items: $0)] }.drive(self.offersCollectionView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
我的 OfferSection 对象如下所示:
struct OfferSection: AnimatableSectionModelType {
var header: String
var items: [Offer]
var identity: Int {
return 0
}
}
extension OfferSection {
typealias Identity = Int
typealias CustomDataAlias = Offer
init(original: OfferSection, items: [Offer]) {
self = original
self.items = items
}
}
我的
Offer
对象符合 Equatable
以及 IdentifiableType
,如文档中所建议。
struct Offer: IdentifiableType, Equatable {
let id: Int?
let name: String
let path: String?
let offerLocation: String
let email: String?
let section: String
let categories: [String]
let uniqueId = UUID().uuidString
var identity: String {
return uniqueId
}
static func == (lhs: Offer, rhs: Offer) -> Bool {
lhs.id == rhs.id && lhs.name == rhs.name && lhs.offerLocation == rhs.offerLocation
}
}
我想知道,也许我做错了什么?
我在您显示的代码中看到了许多问题,可能还有其他问题......
Offer.==
写得不正确。把它去掉,让编译器帮你写。uniqueId
并使用 id
作为 identity
。OfferSection
也写错了。删除它并替换为:typealias OfferSection = AnimatableSectionModel<String, Offer>
如果进行上述更改无法解决您的问题。使用更多信息更新问题。