gpt4 book ai didi

ios - 无法生成具有旧节数 : 1 and new section count: 0 with FetchedResultsController 的新节图

转载 作者:搜寻专家 更新时间:2023-10-31 22:56:34 24 4
gpt4 key购买 nike

在我的 View Controller 中,我有一个已实现的 fetchedResultsController 并且一切正常(显示行、多个部分、获取等)。

但是当我尝试更新或删除部分中的最后一行时出现错误(当我说“最后一行”时,我的意思是该部分中只剩下一行)。如果该部分中有不止一行,当我更新它的值(在 Core Data 中)时,它不会发生任何错误。我读过 this , thisthis但它对我没有帮助(解决方案已过时),而且我还没有在 StackOverflow 和互联网上找到任何解决我的问题的方法。我需要帮助。我正在使用 Xcode 8.3.3 和 Swift 3。

这是我在尝试更新 tableView 的任何部分中的“唯一”行时遇到的错误:

CoreData: error: Serious application error.  
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
UITableView internal bug: unable to generate a new section map with old section count:
1 and new section count: 0 with userInfo (null)

我正在使用此方法返回节数:

  func numberOfSections(in tableView: UITableView) -> Int {

return fetchedResultsController?.sections?.count ?? 0

}

这是获取行数的方法:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if let sections = fetchedResultsController.sections {

let currentSection = sections[section]
return currentSection.numberOfObjects
}

return 0

}

我的 View Controller 中还有以下方法:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.beginUpdates()
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.endUpdates()
}



func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .delete:
guard let path = indexPath
else { return }
tableView.deleteRows(at: [path],
with: .none)

case .insert:
guard let path = newIndexPath
else { return }
tableView.insertRows(at: [path],
with: .automatic)

case .move:

guard let _ = indexPath,
let _ = newIndexPath
else { return }

if indexPath != newIndexPath {
tableView.deleteRows(at: [indexPath!], with: .none)
tableView.insertRows(at: [newIndexPath!], with: .none)
}


case .update:

guard let path = indexPath
else { return }

tableView.reloadRows(at: [path], with: .none) // No blinking with .none

}

}

如果没有部分,我尝试更改 numberOfSections 方法以返回 1 个部分,

func numberOfSections(in tableView: UITableView) -> Int {

if self.fetchedResultsController.sections?.count == 0 {
return 1
} else {
return fetchedResultsController?.sections?.count ?? 1
}

但在这种情况下我遇到了另一个错误。

  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray with userInfo (null)

最佳答案

经过一些尝试,我找到了解决方案:

添加此方法后我没有错误:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
case .delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
case .move:
break
case .update:
break
}
}

关于ios - 无法生成具有旧节数 : 1 and new section count: 0 with FetchedResultsController 的新节图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com