I am using NSFetchedResultsController. I used to have a similar problem, when there are no records in the database to represent the table, but then one is created, I ended up in at least one section there, so I fixed it. But now it crashes when I have, for example, two partitions, each with one row, and I delete one row, so the section should be deleted -> crash. It states that the number of partitions before updating (2) is not equal to the number of deleted (0).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return 1 if the fetchedResultsController section count is zero return [[fetchedResultsController sections] count] ? : 1; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // check if we really have any sections in the managed object: if (!fetchedResultsController.sections.count) return @"Persoonlijk"; id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo name]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // check if we really have any sections in the managed object: if (!fetchedResultsController.sections.count) return 0; id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; }
Update A method in which a row is deleted:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) {
source share