我在UISplitViewController
中有一個UITableViewController
。這tableViewController收到兩件事情:對UITableView的一部分使用NSFetchedResultsController
- 發票
- Customer對象的數組
所以我在做什麼是填充在UITableView中的第一部分(或Section 0
)發票的詳細信息。
對於第二部分,我使用NSFetchedResultsController
來獲取具有invoice == nil
的客戶的所有屬性,並在表視圖的第二部分中顯示該屬性。
所以基本上只使用NSFetchedResultsController
而不是UITableViewController
而不是整個東西。
現在,一切工作正常。但是,當我點擊第2部分的行時,我提供用戶將property
添加到新發票。當我這樣做,我得到這個錯誤:
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期望Section 0,但我確定它被手動強制只使用Section 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)
}
出了什麼問題嗎?
嗨,我在保存'managedObjectContext'之前調用'self.tableView.reloadData()'並在將數據追加到數組之後立即調用。但我仍然遇到同樣的問題。 –
嘿,你是對的。這是發生錯誤的主要原因。現在,我在FRC之前調用'self.tableView.reloadData'甚至注意到這些更改及其所有工作正常。謝謝! –