当我使用 RxDatasources 更新数据源时,不会触发 configureCell
configureCell is not triggered when I update a datasource using RxDatasources
我的视图模型中有一个行为中继用作数据源。它的定义是这样的:
var posts: BehaviorRelay<[PostModel]>
它是通过网络用数据初始化的,我给它绑定数据时它能正常初始化tableView
现在,如果我尝试更改这里 post 的点赞状态,就像这样(这也在我的视图模型中):
private func observeLikeStatusChange() {
self.changedLikeStatusForPost
.withLatestFrom(self.posts, resultSelector: { (, [=11=]) })
.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
struct 符合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: [=12=]) }
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
}
您在 PostCellModel
中错误地定义了 Equatable 一致性。因此,系统无法判断单元格模型是否已更改...删除您手动定义的 ==(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
}
我的视图模型中有一个行为中继用作数据源。它的定义是这样的:
var posts: BehaviorRelay<[PostModel]>
它是通过网络用数据初始化的,我给它绑定数据时它能正常初始化tableView
现在,如果我尝试更改这里 post 的点赞状态,就像这样(这也在我的视图模型中):
private func observeLikeStatusChange() {
self.changedLikeStatusForPost
.withLatestFrom(self.posts, resultSelector: { (, [=11=]) })
.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
struct 符合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: [=12=]) }
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
}
您在 PostCellModel
中错误地定义了 Equatable 一致性。因此,系统无法判断单元格模型是否已更改...删除您手动定义的 ==(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
}