Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating UITableView will cause exception
    text
    copied!<p>I have <code>UITableViewController</code> subclassed and implemented the following</p> <ol> <li><code>NSFetchedResultsController</code> with its delegates</li> <li><code>tableView:sectionForSectionIndexTitle:atIndex:</code></li> <li><code>tableView:titleForHeaderInSection:</code></li> </ol> <p>In <code>controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:</code></p> <pre><code>case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; </code></pre> <p>In data model for the given entity I have transient property <code>uppercaseFirstLetterOfName</code> which will return first letter of persistent property.</p> <p>This is all to achieve Alphabetical sections for table items and side index.</p> <p>Now if I have a single record for a section, then I rename it so it will change the section, I get the following exception, which happens somewhere after <code>NSFetchedResultsChangeMove</code>.</p> <blockquote> <p>*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:774</p> <p>Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) 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, 1 deleted).</p> </blockquote> <p>Any ideas?</p> <p>UPD some more code:</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSInteger count = [[fetchedResultsController sections] count]; return count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger numberOfRows = 0; if ([[fetchedResultsController sections] count] &gt; 0) { id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; numberOfRows = [sectionInfo numberOfObjects]; } return numberOfRows; } </code></pre> <p>...</p> <pre><code> NSSortDescriptor* sortByWordDescriptor = [[NSSortDescriptor alloc] initWithKey:@"subject" ascending:YES]; NSArray* sortArray = [[NSArray alloc] initWithObjects:sortByWordDescriptor, nil]; [fetchRequest setSortDescriptors:sortArray]; NSFetchedResultsController* controller = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"uppercaseFirstLetterOfName" cacheName:@"Root"]; </code></pre> <p>UPD(patched): At the moment I patched the code like this:</p> <pre><code> case NSFetchedResultsChangeMove: NSUInteger tableSectionCount = [self.tableView numberOfSections]; NSUInteger modelSectionCount = [[controller sections] count]; if (modelSectionCount &gt; tableSectionCount) { [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[newIndexPath section]] withRowAnimation:UITableViewRowAnimationNone]; } else if(modelSectionCount &lt; tableSectionCount) { if (tableSectionCount &gt; 1) { [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone]; } } [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; </code></pre> <p>So far no crash, but is this correct solution?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload