gpt4 book ai didi

ios - NSFetchedResultsController 在向第二部分插入行时在第一部分返回错误的行数

转载 作者:行者123 更新时间:2023-11-28 06:35:24 26 4
gpt4 key购买 nike

这是我的 NSFetchedResultsControllerDelegate 和一些打印:

//MARK: - NSFetchedResultsControllerDelegate

func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {

let indexSet = NSIndexSet(index: sectionIndex)

switch type {
case .Insert:

print("SECTION INSERT --->>> \(sectionIndex)")
tableView.insertSections(indexSet, withRowAnimation: .Fade)

case .Delete:

tableView.deleteSections(indexSet, withRowAnimation: .Fade)

case .Update:

fallthrough

case .Move:

tableView.reloadSections(indexSet, withRowAnimation: .Fade)
}
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

switch type {
case .Insert:

if let newIndexPath = newIndexPath {

print("ROW INSERT --->>> \(newIndexPath)")
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}

case .Delete:

if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}

case .Update:

if let indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}

case .Move:

if let indexPath = indexPath, let newIndexPath = newIndexPath {

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
}

print("First sumOfRows --->>> \(fetchedResultsController.fetchedObjects?.count)")

var sumOfRows = 0
for section in fetchedResultsController.sections! {

sumOfRows += section.objects!.count
}

print("Second sumOfRows--->>> \(sumOfRows)")
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}

//MARK: - UITableViewDataSource

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController?.sections?.count ?? 0
}

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

print("Section --->>> \(section)")
print("Number of rows in section --->>> \(fetchedResultsController.sections![section].objects!.count)")
return fetchedResultsController.sections![section].objects!.count
}

插入之前,我只有一个部分,其中一行。

当我插入新行时,控制台上的输出如下:

SECTION INSERT --->>> 1
ROW INSERT --->>> <NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}
First sumOfRows --->>> Optional(2)
Second sumOfRows--->>> 3
Section --->>> 0
Number of rows in section --->>> 2
Section --->>> 1
Number of rows in section --->>> 1

我得到的错误:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

为什么 NSFetchedResultsController 在部分中返回错误的行数?

最佳答案

只需为您的numberOfRowsInSection 方法使用numberOfObjects 而不是objects!.count

请参阅this回答。

关于ios - NSFetchedResultsController 在向第二部分插入行时在第一部分返回错误的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39223113/

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