微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RxDataSources 在数据源内容的每次更改时闪烁

如何解决RxDataSources 在数据源内容的每次更改时闪烁

最初应用程序显示 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
   }
}

解决方法

您的模型可能不符合正确所需的协议,并且差异检查器无法识别相同的细胞模型,因此它会再次呈现整个集合视图。 请参阅相关文档:

‘’’ 支持扩展您的项目和部分结构 只需使用 IdentifiableTypeEquatable 扩展您的项目,并使用 AnimatableSectionModelType 扩展您的部分 '''

https://github.com/RxSwiftCommunity/RxDataSources

此外,您还可以按照此示例 - https://bytepace.medium.com/bring-tables-alive-with-rxdatasources-rxswift-part-1-db050fbc2cf6

,

CloudBalancing 的回答是正确的。您的 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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。