最初,应用程序只显示一个部分的UIViewCollection,然后在接收到内容后出现新的部分。用户滚动集合,新的部分添加到集合的底部。
我使用MVVM,所以在我的ViewModel中我向内容数组(model.content)添加了一个新的部分,然后通知绑定到collectionView.rx.items(dataSource: self.dataSource)的发布者。
所以每次我添加这个部分时,集合都是闪烁的,所有的单元格都是重新加载的,这使用户体验到所有的东西都在闪烁、闪烁、消失和出现。是否有一种方法可以不重新加载所有集合,而只加载不同的集合。我认为它在默认情况下应该这样工作。也许通知BehaviorSubject的方法是错误的?
我也尝试使用RxTableViewSectionedAnimatedDataSource,但是通过这种方式,所有的东西都消失了,并且在每个新的部分都被添加之后,将用户移动到集合视图的最顶端。
请知道为什么所有的收集应该重新加载,如果我只是增加一个部分底部,如何防止它?
typealias ContentDataSource = RxCollectionViewSectionedReloadDataSource<ContentSection>
class ContentViewController: BaseViewController<ContentViewModel> {
func setupBindings() {
viewModel?.sectionItemsSubject
.bind(to: collectionView.rx.items(dataSource: self.dataSource))
.disposed(by: self.disposeBag)
}
}
class ContentViewModel: BaseViewModel<ContentModel> {
lazy var sectionItemsSubject = BehaviorSubject<[ContentSection]>(value: model.content)
func updateGeneratedSection(_ section: ContentSection) {
model.content.append(item)
sectionItemsSubject.onNext(self.model.content)
}
}
struct ContentModel {
var content: [ContentSection] = []
}编辑的
struct ContentSection {
var id: String
var items: [Item]
var order: Int
}
extension ContentSection: SectionModelType {
typealias Item = ItemCellModel
init(original: ContentSection, items: [ItemCellModel]) {
self = original
self.items = items
}
}
struct ItemCellModel {
let id: String
let img: String
init(id: String, img: String) {
self.id = id
self.img = img
}
}发布于 2021-05-06 07:13:12
您的模型可能不符合所需的协议,而diff检查器无法识别它们相同的单元模型,因此它再次呈现整个集合视图。请参阅有关这方面的文件:
“”支持扩展项和节结构,只需使用IdentifiableType和等效扩展项,用AnimatableSectionModelType扩展区段‘“
https://github.com/RxSwiftCommunity/RxDataSources
此外,您还可以遵循这个示例- https://bytepace.medium.com/bring-tables-alive-with-rxdatasources-rxswift-part-1-db050fbc2cf6。
发布于 2021-05-06 21:30:25
克劳德·巴利奇的回答是正确的。你的ContentSection类型错了..。试试这个:
typealias ContentSection = AnimatableSectionModel<ContentSectionModel, ItemCellModel>
struct ContentSectionModel: IdentifiableType {
var identity: String
var order: Int
}
struct ItemCellModel: IdentifiableType, Equatable {
let identity: String
let img: String
}https://stackoverflow.com/questions/67410853
复制相似问题