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