将 NSDiffableDataSourceSnapshot 应用到 UITableViewDiffableDataSource 导致 'NSInternalInconsistencyException'

问题描述

我正在尝试为我的 tableview 实现一个 UITableViewViewDiffableDataSource。我的代码编译得很好,但是我第一次对它应用快照时一直遇到这个错误,并出现以下错误

由于未捕获的异常“NSInternalInconsistencyException”而终止应用,原因:“无效参数不满足:section

这是我的代码

BasedisplayManager

class BasedisplayManager: NSObject,UITableViewDelegate,BaseDiffableDataSourceDelegate {
   var tableData = [[BaseviewmodelProtocol]]()
   var sections: [DiffableSection] = []
...

   func setupDiffableDataSource(forTableView tableView: UITableView) {
      dataSource = BaseDiffableDataSource(tableView: tableView,cellProvider: { (tableView,indexPath,model) -> UITableViewCell? in
          guard let model = model as? BaseviewmodelProtocol else { return nil }
          let cell = tableView.dequeueReusableCell(withIdentifier: model.cellIdentifier,for: indexPath)
          (cell as? TableViewCell)?.model = model
          (cell as? NoSwipeTableViewCell)?.model = model
          return cell
      })

      dataSource.delegate = self
   }

   func reloadSnapshot() {
      var snapshot = NSDiffableDataSourceSnapshot<AnyHashable,AnyHashable>()
      snapshot.appendSections(sections.map { $0.sectionIdentifier() })
      sections.forEach { [weak self] section in
          guard let items = self?.tableData[section.sectionIndex()] as? [AnyHashable] else { return }
          snapshot.appendItems(items,toSection: section.sectionIdentifier())
      }
    
      dataSource.apply(snapshot,animatingDifferences: true)
   }

DiffableSection

protocol DiffableSection {
  func sectionIndex() -> Int
  func sectionIdentifier() -> AnyHashable
}

BaseDiffableDataSource

protocol BaseDiffableDataSourceDelegate: class {
   func tableView(_ tableView: UITableView,canEditRowAt indexPath: IndexPath) -> Bool
   func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath)
   func tableView(_ tableView: UITableView,canMoveRowAt indexPath: IndexPath) -> Bool
   func tableView(_ tableView: UITableView,moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath)
}

class BaseDiffableDataSource: UITableViewDiffableDataSource<AnyHashable,AnyHashable> {

   weak var delegate: BaseDiffableDataSourceDelegate! = nil

   override func tableView(_ tableView: UITableView,canEditRowAt indexPath: IndexPath) -> Bool {
    return delegate.tableView(tableView,canEditRowAt: indexPath)
   }

   override func tableView(_ tableView: UITableView,forRowAt indexPath: IndexPath) {
    return delegate.tableView(tableView,commit: editingStyle,forRowAt: indexPath)
   }

   override func tableView(_ tableView: UITableView,canMoveRowAt indexPath: IndexPath) -> Bool {
    return delegate.tableView(tableView,canMoveRowAt: indexPath)
   }

   override func tableView(_ tableView: UITableView,to destinationIndexPath: IndexPath) {
    return delegate.tableView(tableView,moveRowAt: sourceIndexPath,to: destinationIndexPath)
   }
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)