【发布时间】:2021-03-11 17:52:12
【问题描述】:
我正在尝试应用一个空快照,它使我的应用程序崩溃。我一直在尝试调试它 2 天,但似乎无法找到解决此问题的方法。下面是我正在运行的代码:
//
// ItemsListDiffableVC.swift
// FetchRewardsCodingExercise
//
// Created by Vandan Patel on 11/26/20.
//
import UIKit
final class ItemsListDiffableVC: UIViewController {
private var tableView: UITableView!
private var dataSource: ItemDataSource!
private var groupedItems = [Dictionary<Int, [Item]>.Element]()
var presenter: ItemsListPresentable!
private let cellReusableID = "itemCell"
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
presenter.didLoadView()
}
private func configureTableView() {
tableView = UITableView()
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.backgroundColor = .systemGroupedBackground
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReusableID)
view.addSubview(tableView)
}
private func configureDataSource() {
dataSource = ItemDataSource(tableView: self.tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: self.cellReusableID, for: indexPath) as! ItemCell
cell.configureCell(withTitle: item.name ?? "")
return cell
})
}
}
extension ItemsListDiffableVC: ItemsListViewable {
func display(groupedItems: [Dictionary<Int, [Item]>.Element]) {
DispatchQueue.main.async {
self.configureDataSource()
self.update(with: groupedItems)
}
}
func display(error: String) {
}
}
extension ItemsListDiffableVC {
private func update(with groupedItems: [Dictionary<Int, [Item]>.Element]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
dataSource.apply(snapshot, animatingDifferences: true, completion: nil)
}
}
class Section: Hashable {
let sectionName: String
let identifier = UUID()
init(sectionName: String) {
self.sectionName = sectionName
}
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
static func == (lhs: Section, rhs: Section) -> Bool {
return lhs.identifier == rhs.identifier
}
}
class ItemDataSource: UITableViewDiffableDataSource<Section, Item> {
var groupedItems = [Dictionary<Int, [Item]>.Element]()
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return groupedItems[section].key.sectionTitle
}
}
struct Item: Codable, Hashable {
let id: Int
let listId: Int
let name: String?
}
这是我得到的错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (0) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
我不明白的是为什么在更新之前还有一个部分以及如何处理它。
谢谢。
【问题讨论】:
-
什么是物品?您的数据在哪里?应用空快照的目的是什么?请出示您的真实代码并说明您的真实目标。
-
另外,
[Dictionary<Int, [Item]>.Element]是一种奇怪的说话方式。 -
我只是想帮助找出应用程序崩溃的原因。我应用了一个空快照,以便我们可以轻松地看到 1 个错误。即使我正在应用一个空快照,但错误说它甚至在更新之前就有 1 个部分。这就是我想要弄清楚的。 1、为什么我一初始化tableView就变成了1个section? 2.如果是1,怎么变成0?谢谢!
-
好吧,你没有提供足够的代码来测试这个问题。请提供一个可运行的、可编译的、独立的示例来执行崩溃。
-
您的代码仍然无法编译。什么是 ItemsListPresentable?什么是 ItemCell?什么是 ItemsListViewable?
sectionTitle属性从何而来?不能编译就不能运行,不能运行就不能崩溃。
标签: ios swift uitableview uitableviewdiffabledatasource