我很难理解 DiffableDataSource 是如何工作的。
我有这样的 ViewModel
struct ViewModel: Hashable {
var id: Int
var value: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
我有 tableView 由 cachedItems 填充,如上面的 ViewModele。当 API 响应到达时,我想添加一个新的,删除缺失的一个,刷新 tableView 中已经存在的项目的 viewModel.value 并最终订购它。
除了一件事 - 重新加载项目外,一切正常。
我对 DiffableDataSource 的理解是它比较 item.hash() 以检测项目是否已经存在,如果存在,那么如果 cachedItem != apiItem,它应该重新加载。
不幸的是,这不起作用,快照确实删除和插入而不是重新加载。
DiffableDataSource 应该这样做吗?
当然,我有一个解决方案 - 为了使它工作,我需要遍历 cachedItems,当新项目包含相同的 id 时,我更新 cachedItem,然后我在没有动画的情况下应用快照,然后我终于可以应用带有动画的删除/插入/订购动画。
但是这个解决方案似乎更像是一个黑客而不是一个有效的代码。有没有更清洁的方法来实现这一目标?
更新:
有代码显示问题。它应该在操场上工作。
例如。 items 和 newItems 包含 id == 0 的 viewModel。
Hash 是相同的,因此 diffableDataSource 应该重新加载,因为字幕不同。
但是有可见的删除/插入而不是重新加载
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
let tableView = UITableView()
var diffableDataSource: UITableViewDiffableDataSource<Section, ViewModel>?
enum SelectesItems {
case items
case newItems
}
var selectedItems: SelectesItems = .items
let items: [ViewModel] = [ViewModel(id: 0, title: "Title1", subtitle: "Subtitle2"),
ViewModel(id: 1, title: "Title2", subtitle: "Subtitle2"),
ViewModel(id: 2, title: "Title3", subtitle: "Subtitle3"),
ViewModel(id: 3, title: "Title4", subtitle: "Subtitle4"),
ViewModel(id: 4, title: "Title5", subtitle: "Subtitle5")]
let newItems: [ViewModel] = [ViewModel(id: 0, title: "Title1", subtitle: "New Subtitle2"),
ViewModel(id: 2, title: "New Title 2", subtitle: "Subtitle3"),
ViewModel(id: 3, title: "Title4", subtitle: "Subtitle4"),
ViewModel(id: 4, title: "Title5", subtitle: "Subtitle5"),
ViewModel(id: 5, title: "Title6", subtitle: "Subtitle6")]
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellID")
diffableDataSource = UITableViewDiffableDataSource<Section, ViewModel>(tableView: tableView, cellProvider: { (tableView, indexPath, viewModel) -> UITableViewCell? in
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "CellID")
cell.textLabel?.text = viewModel.title
cell.detailTextLabel?.text = viewModel.subtitle
return cell
})
applySnapshot(models: items)
let tgr = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tgr)
}
@objc func handleTap() {
switch selectedItems {
case .items:
applySnapshot(models: items)
selectedItems = .newItems
case .newItems:
applySnapshot(models: newItems)
selectedItems = .items
}
}
func applySnapshot(models: [ViewModel]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, ViewModel>()
snapshot.appendSections([.main])
snapshot.appendItems(models, toSection: .main)
diffableDataSource?.apply(snapshot, animatingDifferences: true)
}
}
enum Section {
case main
}
struct ViewModel: Hashable {
let id: Int
let title: String
let subtitle: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
最佳答案
这是因为您错误地实现了 Hashable。
请记住,Hashable 也意味着 Equatable——两者之间有着不可侵犯的关系。规则是两个相等的对象必须具有相等的哈希值。但是在您的 ViewModel 中,“相等”涉及比较所有三个属性,id
, title
, 和 subtitle
— 尽管 hashValue
没有,因为你实现了 hash
.
换句话说,如果你实现 hash
,您必须执行 ==
完全匹配:
struct ViewModel: Hashable {
let id: Int
let title: String
let subtitle: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {
return lhs.id == rhs.id
}
}
如果您进行更改,您会发现表格 View 动画的行为符合您的预期。
如果您还希望表 View 了解底层数据实际上已更改的事实,那么您还必须调用
reloadData
。 : diffableDataSource?.apply(snapshot, animatingDifferences: true) {
self.tableView.reloadData()
}
(如果您有其他原因希望 ViewModel 的 Equatable 继续涉及所有三个属性,那么您需要两种类型,一种用于执行简单而简单的相等比较时使用,另一种用于涉及 Hashable 的上下文,例如 diffable 数据源,集合和字典键。)
关于Swift DiffableDataSource 进行插入和删除而不是重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62267256/