首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用configureCell更新数据源时不会触发RxDatasources

使用configureCell更新数据源时不会触发RxDatasources
EN

Stack Overflow用户
提问于 2021-12-24 18:09:52
回答 1查看 185关注 0票数 1

我的视图模型中有一个作为数据源的行为中继。其定义如下:

代码语言:javascript
复制
var posts: BehaviorRelay<[PostModel]>

它是通过网络使用数据初始化的,当我将数据绑定到它时,它通常会初始化tableView。

现在,如果我试图改变一个帖子的类似状态,就像这样(在我看来,这也是模型):

代码语言:javascript
复制
private func observeLikeStatusChange() {
        
        self.changedLikeStatusForPost
            .withLatestFrom(self.posts, resultSelector: { ($1, $0) })
            .map{ (posts, changedPost) -> [PostModel] in
                //...
                var editedPosts = posts
                editedPosts[index] = changedPost // here data is correct, index, changedContact
                return editedPosts
            }
            .bind(to: self.posts)
            .disposed(by: disposeBag)
    }

所以这样,什么都不会发生。如果从editedPosts中删除元素,则tableView将正确更新并删除该行。

PostModel结构符合Equatable,目前它要求所有属性都是相同的。

在我的视图控制器中,我创建了如下数据源:

代码语言:javascript
复制
tableView.rx.setDelegate(self).disposed(by: disposeBag)
 let dataSource = RxTableViewSectionedAnimatedDataSource<PostsSectionModel>(
            configureCell: { dataSource, tableView, indexPath, item in
             //...
                return postCell
            })
        
        postsViewModel.posts
            .map({ posts in
                let models = posts.map{ PostCellModel(model: $0) }
                return [PostsSectionModel(model: "", items: models)]
            }) // If I put debug() here, this is triggered and I get correct section model with correct values
            .bind(to: self.tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)

所以,正如我所说的,我得到了正确的值,但是没有触发configureCell。我在这里做错了什么?

编辑:

以下是PostCellModel:

进口基金会

代码语言:javascript
复制
import RxDataSources

typealias PostsSectionModel = AnimatableSectionModel<String, PostCellModel>


struct PostCellModel : Equatable, IdentifiableType {
    
    static func == (lhs: PostCellModel, rhs: PostCellModel) -> Bool {
        
        return lhs.model.id == rhs.model.id
    }
    
    var identity: Int {
        return model.id
    }
    var model: PostModel
}

和一个PostModel:

代码语言:javascript
复制
 struct PostModel: Codable, CellDataModel, Equatable {
    static func == (lhs: PostModel, rhs: PostModel) -> Bool {
       return
        lhs.liked == rhs.liked &&
        rhs.title == lhs.title &&
        
        lhs.location == rhs.location &&
        lhs.author == rhs.author &&
       
        lhs.created == rhs.created
    }
    
    let id: Int
    let title: String
    let location: String?
    let author: String
    let created: Int
    let liked:Bool
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-25 15:16:06

您在PostCellModel中错误地定义了您的等效一致性。因此,系统无法判断单元模型是否发生了变化.删除您手动定义的==(lhs:rhs:),让系统为您生成它们,您应该很好.

代码语言:javascript
复制
typealias PostsSectionModel = AnimatableSectionModel<String, PostCellModel>

struct PostCellModel : Equatable, IdentifiableType {
    var identity: Int {
        return model.id
    }
    var model: PostModel
}

struct PostModel: Codable, CellDataModel, Equatable {
    let id: Int
    let title: String
    let location: String?
    let author: String
    let created: Int
    let liked:Bool
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70475576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档