【发布时间】:2022-01-01 20:47:21
【问题描述】:
我有一个从服务器获取的模型列表,基本上我得到了这些模型的数组:
struct ContactModel: Codable, Equatable {
static func == (lhs: ContactModel, rhs: ContactModel) -> Bool {
return lhs.name == rhs.name &&
lhs.avatar == rhs.avatar &&
lhs.job == rhs.job &&
lhs.follow == rhs.follow
}
let id: Int
let name: String
let avatar:String?
let job: JobModel?
let follow:Bool
enum CodingKeys: String, CodingKey {
case id, name, avatar, job, follow
}
}
所以我想在我的tableview 中显示联系人列表。
现在我也有了这个结构,它是这个模型的包装:
struct ContactCellModel : Equatable, IdentifiableType {
static func == (lhs: ContactCellModel, rhs: ContactCellModel) -> Bool {
return lhs.model == rhs.model
}
var identity: Int {
return model.id
}
var model: ContactModel
var cellIdentifier = ContactTableViewCell.identifier
}
我要做的是使用RxDatasources 创建数据源,并绑定到它,如下所示(ContactsViewController.swift):
let dataSource = RxTableViewSectionedAnimatedDataSource<ContactsSectionModel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier: item.cellIdentifier, for: indexPath) as? BaseTableViewCell{
cell.setup(data: item.model)
return cell
}
return UITableViewCell()
})
但我不确定之后应该做什么。我尝试过这样的事情:
Observable.combineLatest(contactsViewModel.output.contacts, self.contactViewModel.changedStatusForContact)
.map{ (allContacts, changedContact) -> ContactsSectionModel in
//what should I return here?
}.bind(to: dataSource)
我使用combineLatest,因为我还有一个可观察的 (self.contactViewModel.changedStatusForContact) 来通知某个联系人已更改(当您点击联系人单元格上的某个按钮时会发生这种情况)。
那么我应该从上面的 .map 返回什么才能成功绑定到之前创建的dataSource?
【问题讨论】:
标签: swift tableview rx-swift rxdatasources