我的視圖模型中有一個行為中繼,用作資料源。它的定義是這樣的:
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則會正確更新并洗掉該行。
PostModelstruct 符合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
}
uj5u.com熱心網友回復:
您在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://www.uj5u.com/caozuo/393998.html
