首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDiffableDataSourceSnapshot在UITableViewDiffableDataSource事业'NSInternalInconsistencyException‘中的应用

NSDiffableDataSourceSnapshot在UITableViewDiffableDataSource事业'NSInternalInconsistencyException‘中的应用
EN

Stack Overflow用户
提问于 2021-04-06 10:56:37
回答 1查看 190关注 0票数 0

我正在尝试为我的表视图实现一个UITableViewViewDiffableDataSource。我的代码编译得很好,但是当我第一次将快照应用到它时,我仍然会遇到这个错误,并出现以下错误:

由于

异常“NSInternalInconsistencyException”终止应用程序,原因:“无效参数不令人满意:节< _state.dataSourceSnapshot numberOfSections”

这是我的代码:

BaseDisplayManager

代码语言:javascript
复制
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

代码语言:javascript
复制
protocol DiffableSection {
  func sectionIndex() -> Int
  func sectionIdentifier() -> AnyHashable
}

BaseDiffableDataSource

代码语言:javascript
复制
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, commit editingStyle: UITableViewCell.EditingStyle, 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, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    return delegate.tableView(tableView, moveRowAt: sourceIndexPath, to: destinationIndexPath)
   }
}

}

EN

回答 1

Stack Overflow用户

发布于 2021-09-06 14:34:26

我在一个ViewController中遇到了同样的问题,它是UITableViewController的一个子类。

对我来说,我是在配置我的数据源,并在viewWillAppear()中应用快照并调用super.viewWillAppear()。取消超级电话是个好办法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66967311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档