我的视图模型中有一个作为数据源的行为中继。其定义如下:
var posts: BehaviorRelay<[PostModel]>它是通过网络使用数据初始化的,当我将数据绑定到它时,它通常会初始化tableView。
现在,如果我试图改变一个帖子的类似状态,就像这样(在我看来,这也是模型):
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,目前它要求所有属性都是相同的。
在我的视图控制器中,我创建了如下数据源:
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:
进口基金会
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:
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
}发布于 2021-12-25 15:16:06
您在PostCellModel中错误地定义了您的等效一致性。因此,系统无法判断单元模型是否发生了变化.删除您手动定义的==(lhs:rhs:),让系统为您生成它们,您应该很好.
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
}https://stackoverflow.com/questions/70475576
复制相似问题