我有一条崩溃错误消息 - 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:itemIdentifier”
我使用 UITableViewDiffableDataSource 来实现我的 UITableView。当我尝试“dataSource?.apply”时发生了崩溃。它只发生在 iPad 上,iPhone 工作正常
您知道什么是原因以及如何解决吗?
代码示例
class myVC: UIViewController {
struct Section: Hashable {
enum Kind: Hashable, CaseIterable {
case `default`
case full
}
let title: String? = nil
let kind: Kind
let items: [Item]
}
struct Item: Hashable {
let title: String?
let detailTitle: String?
let subDetailTitle: String?
let image: UIImage?
let isActionable: Bool
let action: ((Any?) -> Void)?
}
private typealias DataSource = UITableViewDiffableDataSource<Section, Item>
private typealias Snapshot = NSDiffableDataSourceSnapshot<Section, Item>
private var dataSource: DataSource?
private var flag: Bool = false
func setData(sections: [TimeLogDetailViewController.Section]) {
var snapshot = Snapshot()
sections.forEach { section in
snapshot.appendSections([section])
snapshot.appendItems(section.items, toSection: section)
}
dataSource?.apply(snapshot, animatingDifferences: false)
}
func configureSections() -> [TimeLogDetailViewController.Section] {
var sections = [TimeLogDetailViewController.Section]()
if !flag {
sections.append(configFirstSection())
} else {
sections.append(emptySection(kind: .default))
sections.append(configSecondSection())
}
return sections
}
func emptySection(kind: TimeLogDetailViewController.Section.Kind) -> TimeLogDetailViewController.Section {
Section(kind: kind, items: [])
}
func configFirstSection() -> [Item] {
[Item(title: "Title"))]
}
func configHeaderSectionItems() -> [Item] {
return configFirstSection().append([Item(title: "SecondTitle")])
}
}