gpt4 book ai didi

ios - 在 UITableView 的一部分上使用 NSFetchedResultsController

转载 作者:行者123 更新时间:2023-11-30 13:31:50 25 4
gpt4 key购买 nike

我在 UISplitViewController 中有一个 UITableViewController。这个 tableViewController 接收两件事:

  1. 发票数组
  2. 客户对象

所以我正在做的是在 UITableView 的第一部分(或 Section 0)中填充发票详细信息。

对于第二部分,我使用 NSFetchedResultsController 来获取具有 invoice == nil 的客户的所有属性,并将其显示在表格 View 的第二部分中.

所以基本上,仅在 UITableViewController 的一部分而不是整个事物上使用 NSFetchedResultsController

现在,一切正常。但是,当我点击第 2 部分的行时,我会要求用户将该属性添加到新发票中。当我这样做时,我收到此错误:

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)

不知道为什么会发生这种情况。我知道 fetchedResultsController 需要第 0 节,但我已确保手动强制它仅使用第 1 节。

引用代码为:

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
let editedNewIndexPath = NSIndexPath(forRow: indexPath.row, inSection: 1)
tableView.insertRowsAtIndexPaths([editedNewIndexPath], withRowAnimation: .Fade)
}
break;
case .Delete:
if let indexPath = indexPath {
let editedCurrentIndexPath = NSIndexPath(forRow: indexPath.row, inSection: 1)
tableView.deleteRowsAtIndexPaths([editedCurrentIndexPath], withRowAnimation: .Fade)
}
break;
case .Update:
if let indexPath = indexPath {
let editedCurrentIndexPath = NSIndexPath(forRow: indexPath.row, inSection: 1)
if let cell = tableView.cellForRowAtIndexPath(editedCurrentIndexPath){
configureCell(cell, atIndexPath: editedCurrentIndexPath)
//tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .None)
}
}
break;
case .Move:
if let indexPath = indexPath {
let editedCurrentIndexPath = NSIndexPath(forRow: indexPath.row, inSection: 1)
tableView.deleteRowsAtIndexPaths([editedCurrentIndexPath], withRowAnimation: .Fade)
}

if let newIndexPath = newIndexPath {
let editedNewIndexPath = NSIndexPath(forRow: newIndexPath.row, inSection: 1)
tableView.insertRowsAtIndexPaths([editedNewIndexPath], withRowAnimation: .Fade)
}
break;
}
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0{
if let ivData = self.invoices{
print("Section0 - \(ivData.count)")
return ivData.count
}
}
else if section == 1{
if let sections = fetchedResultsController.sections {
let sectionInfo = sections[0]
print("Section1 - \(sectionInfo.numberOfObjects)")
if sectionInfo.numberOfObjects > 0{
return sectionInfo.numberOfObjects
}
else{
return 1
}
}
}
return 0
}

保存新发票的代码是:

do{
self.invoices!.append(billRecord as! InvoiceInfo)
try billRecord.managedObjectContext?.save()
try self.customer!.managedObjectContext?.save()
try record.managedObjectContext?.save()
//self.lookUpLotsWithoutBills()
self.tableView.reloadData()

}
catch{
self.invoices!.removeObject(ivRecord)
let alertController = UIAlertController(title: "Unexpected Error", message: "Your data could not be updated. Please try again later.", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Done", style: .Cancel, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}

这里可能出了什么问题?

最佳答案

看起来你的 FRC 工作正常,但是你的代码没有告诉 TableView 有关新发票行被添加到第 0 节的信息。目前尚不清楚原因,但我会猜测它的原因因为 FRC 在您添加发票之后但在您调用表上的重新加载数据之前触发更新。此时第 0 部分的行计数是错误的,并且您还没有告诉表这一点。

关于ios - 在 UITableView 的一部分上使用 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36492351/

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