I've problems with my custom sectionHeaderView. I'm getting the following error:
error: Serious application error. An exception was caught from the
delegate of NSFetchedResultsController during a call to
-controllerDidChangeContent:.
Invalid update: invalid number of sections. The number of sections contained in the table view after
the update (2) must be equal to the number of sections contained in
the table view before the update (2), plus or minus the number of
sections inserted or deleted (1 inserted, 0 deleted). with userInfo
(null)
If I use the normal titleForHeaderInSection, everything's working fine.
Changing to viewForHeaderInSection causes the error. That's the code for viewForHeaderinSection:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let currSection = fetchedResultsController.sections?[section] else { return nil }
let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: ContractListHeaderView.reuseIdentifier) as! ContractListHeaderView
print(currSection.name)
headerView.sectionLabel.text = currSection.name
return headerView
}
And that's my fetchedResultsController:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
guard let newIndexPath = newIndexPath else { return }
tableView.insertRows(at: [newIndexPath], with: .automatic)
case .delete:
guard let indexPath = indexPath else { return }
tableView.deleteRows(at: [indexPath], with: .automatic)
case .update:
guard let indexPath = indexPath else { return }
tableView.reloadRows(at: [indexPath], with: .automatic)
case .move:
guard let indexPath = indexPath, let newIndexPath = newIndexPath else { return }
if indexPath == newIndexPath {
tableView.reloadData()
} else {
tableView.moveRow(at: indexPath, to: newIndexPath)
}
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
break;
}
}
Thanks for the help
Related
I have 2 ViewControllers:
VC1 populates its tableView based on CoreData attribute isPicked, which is bool and show only items with true state. VC2 is a second Modal (not Full Screen) View Controller which allow user to change the state of isPicked attribute: check and uncheck item (make it true or false). The idea is user picks needed items end dismiss the VC2 and the items should appear in VC1.
I have implemented NSFetchedResultsController to both VC1 and VC2. And as soon as I press on first item (i.e. change isPicked state to true) I receive the error from VC1:
[error] fault: 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).
Here is how I change a state of item in VC2 (true\false):
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let currencyArray = fetchedResultsController.fetchedObjects else { return }
let cell = tableView.cellForRow(at: indexPath) as! PickCurrencyTableViewCell
for currency in currencyArray {
if currency.shortName == cell.shortName.text {
currency.isPicked = !currency.isPicked
coreDataManager.save()
}
}
}
Here is my VC1 NSFetchedResultsController implementation:
override func viewDidLoad() {
super.viewDidLoad()
setupFetchedResultsController()
}
func setupFetchedResultsController() {
let predicate = NSPredicate(format: "isForConverter == YES")
fetchedResultsController = createCurrencyFetchedResultsController(and: predicate)
fetchedResultsController.delegate = self
try? fetchedResultsController.performFetch()
}
func createCurrencyFetchedResultsController(with request: NSFetchRequest<Currency> = Currency.fetchRequest(), and predicate: NSPredicate? = nil, sortDescriptor: [NSSortDescriptor] = [NSSortDescriptor(key: "shortName", ascending: true)]) -> NSFetchedResultsController<Currency> {
request.predicate = predicate
request.sortDescriptors = sortDescriptor
return NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
}
Here is my VC1 NSFetchedResultsController delegate:
extension VC1: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
if let indexPath = indexPath, let newIndexPath = newIndexPath {
switch type {
case .update:
tableView.reloadRows(at: [indexPath], with: .none)
case .move:
tableView.moveRow(at: indexPath, to: newIndexPath)
case .delete:
tableView.deleteRows(at: [indexPath], with: .none)
case .insert:
tableView.insertRows(at: [indexPath], with: .none)
default:
tableView.reloadData()
}
}
}
}
When I reload the app, picked item shows itself in VC1 (which caused crash). But every change in VC2 crashes the app again with the same error. I don't have any methods to delete items in VC1 or so. I need that VC1 tableView just show or hide items according to isPicked state made from VC2.
What I missed in my code?
UPDATE: my VC1 TableView methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let currency = fetchedResultsController.sections![section]
return currency.numberOfObjects
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "converterCell", for: indexPath) as! ConverterTableViewCell
let currency = fetchedResultsController.object(at: indexPath)
cell.shortName.text = currency.shortName
cell.fullName.text = currency.fullName
return cell
}
Doc states:
controller(_:didChange:at:for:newIndexPath:)
indexPath
The index path of the changed object (this value is nil for
insertions).
So, you should change the code like this:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .none)
}
case .move:
if let indexPath = indexPath {
tableView.moveRow(at: indexPath, to: newIndexPath)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
default:
tableView.reloadData()
}
}
}
Need your help guys !
I'm going mad on a bug on iOS 9 only.
As you can see below, it is going well on iOS 11
At the beginning, i've got 4 cells with a status "Nouveau" for new.
When I click on a "new" cell, the status change to "En attente" (for "waiting").
On iOS 9, everything is fine until the last cell. When I tap on the last "new" cell, it doesn't move to "waiting" and the "new" section is still there. After this, the table view is not getting updates anymore, it is going back to normal when i change to another viewController and go back to the one containing the table view.
I'm using NSFetchedResultsController. Data is good, good number of sections, good number of elements by section, good title.
At the start 1 sections with title "Nouveau" with 4 objects
At the end, 1 sections with title "En attente" with 4 objects
extension CRDashboardOrdersViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
print( self.fetchResult?.sections?.count ?? 0 )
return self.fetchResult?.sections?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print( self.fetchResult?.sections?.get(index: section)?.numberOfObjects ?? 0 )
return self.fetchResult?.sections?.get(index: section)?.numberOfObjects ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CRDashboardOrderTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CRDashboardOrderTableViewCell") as! CRDashboardOrderTableViewCell
let order: Order = self.fetchResult.object(at: indexPath) as! Order
cell.order = order
if let selectedIndexPath = self.selectedIndexPath, selectedIndexPath == indexPath {
self.tableView.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}
return cell
}
}
extension CRDashboardOrdersViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndexPath = indexPath
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
print( self.fetchResult?.sections?.get(index: section)?.name ?? "???" )
return self.fetchResult?.sections?.get(index: section)?.name ?? "???"
}
}
extension CRDashboardOrdersViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
print(sectionIndex)
print(sectionInfo.indexTitle)
switch type {
case .insert:
self.tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
self.tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
break
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .update:
self.tableView.reloadRows(at: [indexPath!], with: .fade)
if indexPath == self.selectedIndexPath {
self.selectedIndexPath = indexPath
}
break
case .insert:
self.tableView.insertRows(at: [newIndexPath!], with: .fade)
self.selectedIndexPath = nil
break
case .delete:
self.tableView.deleteRows(at: [indexPath!], with: .fade)
self.selectedIndexPath = nil
break
case .move:
self.tableView.moveRow(at: indexPath!, to: newIndexPath!)
if indexPath == self.selectedIndexPath {
self.selectedIndexPath = newIndexPath
}
break
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.endUpdates()
}
}
You have made a mistake with your cellForRow: and your FetchedResults delegate.
When using a FetchedResultsController, on .update you should not use reloadRows, this causes problems, instead you should have a method which updates a cells model like so:
func updateCell(atPath indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? MyCell {
updateCell(withCell: cell, atIndexPath: indexPath)
}
}
func updateCell(withCell cell: MyCell, atIndexPath indexPath: IndexPath) {
if let MyModel = fetchedResultsController?.object(at: indexPath) {
cell.model = myModel
}
}
Then in your delegate .update you call updateCell(AtIndexPath), to update the cells UI.
You should also use this cell configure method in your cellForRow
This is my NSFetchedResultsControllerDelegate with some prints:
//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
}
BEFORE the insert I have only one section with one row there.
When I insert new row the output on console is following:
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
Error I get:
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)
Why NSFetchedResultsController returns wrong number of rows in section?
Just use numberOfObjects instead of objects!.count for your numberOfRowsInSection method.
Please see this answer.
good morning.
In my App, I have different Entities and I want to show it in the same TableView.
I know that to do it, I need different NSFetchedResultsController.
When I add an object do the first section, no problem appears, but when I add an object to the second, the problem appears.
Here is the code the I'm using
import UIKit
import CoreData
class MainMenu: UITableViewController, NSFetchedResultsControllerDelegate {
/**********/
/** Vars **/
/**********/
lazy var frcAccount: NSFetchedResultsController = self.AccountFetchedResultController()
lazy var frcCar: NSFetchedResultsController = self.CarFetchedResultsController()
let idxAccNew = NSIndexPath(forRow: 0, inSection: 0)
let idxCarNew = NSIndexPath(forRow: 0, inSection: 1)
let idxConfig = NSIndexPath(forRow: 0, inSection: 2)
/**************/
/** IBOutlet **/
/**************/
/***************/
/** IBActions **/
/***************/
/***************/
/** Functions **/
/***************/
// MARK:- NSFetchedResultsController
func AccountFetchedResultController() -> NSFetchedResultsController {
let fetchRequest = NSFetchRequest(entityName: Account.entityName)
fetchRequest.fetchBatchSize = 10
let sortDescriptor = NSSortDescriptor(key: "id", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
frcAccount = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: coreDataStack.context,
sectionNameKeyPath: nil,
cacheName: nil)
frcAccount.delegate = self
do {
try frcAccount.performFetch()
} catch {
print("Erro: \(error)")
abort()
}
return frcAccount
}
func CarFetchedResultsController() -> NSFetchedResultsController {
let fetchRequest = NSFetchRequest(entityName: "Car")
fetchRequest.fetchBatchSize = 10
let sortDescriptor = NSSortDescriptor(key: "plate", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
frcCar = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: coreDataStack.context,
sectionNameKeyPath: nil,
cacheName: nil)
frcCar.delegate = self
do {
try frcCar.performFetch()
} catch {
print("Error: \(error)")
abort()
}
return frcCar
}
//MARK:- TableViewDataSource
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section{
case 0:
return frcAccount.sections![0].numberOfObjects + 1
case 1:
return frcCar.sections![0].numberOfObjects + 1
default:
return 1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") //as UITableViewCell
cell!.textLabel?.text = "Nova Conta"
return cell!
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("CellAccount") as! VC_AccountListTableViewCell
let idx = NSIndexPath(forRow: indexPath.row - 1, inSection: 0)
let object = frcAccount.objectAtIndexPath(idx) as! Account
cell.useAccountInfo(object)
return cell
}
} else if indexPath.section == 1{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
cell!.textLabel?.text = "Novo Carro"
return cell!
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("CellCar") as! VC_CarListTableViewCell
let idx = NSIndexPath(forRow: indexPath.row - 1, inSection: 0)
let object = frcCar.objectAtIndexPath(idx) as! Car
cell.useCarInfo(object)
cell.btnFillUp.tag = indexPath.row - 1
cell.btnService.tag = indexPath.row - 1
return cell
}
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
cell!.textLabel?.text = "Configurações"
return cell!
}
}
// MARK: TableViewDelegate
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath == idxAccNew || indexPath == idxCarNew || indexPath == idxConfig {
return 44
} else {
if indexPath.section == 0 {
return 80
} else if indexPath.section == 1 {
return 100
} else {
return 0
}
}
}
// MARK:- NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller:NSFetchedResultsController) {
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
tableView.endUpdates()
}
func reloadData() {
tableView.reloadData()
}
}
Is it possible to do this?? I have tried lots of different solutions..
Thanks all
In the tableView datasource methods, you are correctly remapping the tableView's indexPath to the relevant FRC's indexPath, whilst also adjusting for the additional first row, eg:
let idx = NSIndexPath(forRow: indexPath.row - 1, inSection: 0)
(Note that you will have to use similar remapping in other tableView datasource delegate methods, e.g. if you implement didSelectRowAtIndexPath). But you also need to do the reverse mapping in the FRC delegate methods, eg.
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
var tvIdx = NSIndexPath()
var newTvIdx = NSIndexPath()
var tvSection = 0
// for the Car FRC, we need to insert/delete rows in table view section 1:
if (controller == self.frcCar) {
tvSection = 1
}
// adjust the indexPaths (indexPath and newIndexPath)
// to add the extra row, and use the correct tableView section:
if let idx = indexPath {
tvIdx = NSIndexPath(forRow: idx.row + 1, inSection: tvSection)
}
if let newIdx = newIndexPath {
newTvIdx = NSIndexPath(forRow: newIdx.row + 1, inSection: tvSection)
}
switch (type) {
case .Insert:
tableView.insertRowsAtIndexPaths([newTvIdx], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([tvIdx], withRowAnimation: .Fade)
case .Move:
tableView.deleteRowsAtIndexPaths([tvIdx], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newTvIdx], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([tvIdx], withRowAnimation: .Fade)
}
}
There is then no need to use reloadData in controllerDidChangeContent:
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
In UITableViewController I can insert row and section at the same time with this implementations:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
if controller == frc {
switch(type) {
case .Insert:
self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case .Update:
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
default:
break
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
What about UICollectionViewController? I haven't found alternative way to implement controllerWillChangeContent and controllerDidChangeContent.
collectionView works somewhat different than tableView, you need to use performBatchUpdates
var frc: NSFetchedResultsController?
var iip = [NSIndexPath]()
var dip = [NSIndexPath]()
var ins: NSIndexSet?
var des: NSIndexSet?
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
if controller == frc {
switch(type) {
case .Insert:
iip.append(newIndexPath)
case .Delete:
dip.append(indexPath)
case .Update:
self.collectionView!.reloadItemsAtIndexPaths([indexPath])
default:
break
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch(type) {
case .Insert:
ins = NSIndexSet(index: sectionIndex)
case .Delete:
des = NSIndexSet(index: sectionIndex)
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.collectionView!.performBatchUpdates({
self.collectionView!.insertItemsAtIndexPaths(self.iip)
self.collectionView!.deleteItemsAtIndexPaths(self.dip)
if self.ins != nil {
self.collectionView!.insertSections(self.ins!)
}
if self.des != nil {
self.collectionView!.deleteSections(self.des!)
}
}, completion: {completed in
self.iip.removeAll(keepCapacity: false)
self.dip.removeAll(keepCapacity: false)
self.ins = nil
self.des = nil
})
}
I was able to do this by checking to see if the section of the new index path is greater than the number of sections in the collection view, and then performing batch updates.
let indexPath = IndexPath()
// index path of item to be inserted
if indexPath.section > collectionView.numberOfSections - 1 {
collectionView.performBatchUpdates({
let set = IndexSet(integer: sectionIndex)
collectionView.insertSections(set)
self.collectionView.insertItems(at: [indexPath])
}, completion: nil)
} else {
collectionView.insertItems(at: [indexPath])
}