BBruce
BBruce

Reputation: 265

Grouped tableview - new entry needs new section. Using Core Data

In my app, I have a grouped tableview that loads from core data. I am using a short date from the fetched results to create the sections. The user can add events by selecting what to add from a modal view.

So it might have a list like this (where dates are section headings):

09-01-2014
  John D.
  Bob S.
  Tim S.
08-27-2014
  Bill B.
  John D.
08-19-2014
  Jane D.
  Tim S.
...  

In this case, the dates are the day that the person was added. Like a login date...

I am handing the selection of people by presenting a collection of people in the database modally and passing the selected person back to my view controller with a protocol method:

-(void)ViewController:(UIViewController *)sender didSelectPlayer:(Player *)selectedPlayer 
{
  CheckIn *newCheckIn = [NSEntityDescription insertNewObjectForEntityForName:@"CheckIn"
                                                    inManagedObjectContext:[self managedObjectContext]];

  newCheckIn.checkedInPlayer = selectedPlayer;
  newCheckIn.checkInTime = [NSDate date];
  newCheckIn.player = [UtilityMethods nameForLabelsWithFirstName:selectedPlayer.firstName withLastName:selectedPlayer.lastName];
  newCheckIn.checkedInPlayer.timeOfLastCheckIn = newCheckIn.checkInTime;
  [newCheckIn.checkedInPlayer setIsCheckedIn:[NSNumber numberWithBool:YES]];
  [newCheckIn setSortDate:[UtilityMethods shortDatefromDate:newCheckIn.checkInTime]];

  [super Save];
}

Nothing special, just setting the properties of the record I am adding.

Note that my entity has attributes: checkInTime and sortDate. The sortDate is just a short date from the checkInTime. it is the attribute that is used to build the sections though...

In my fetchedResultsController:

...
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchedRequest
                                                                managedObjectContext:context
                                                                  sectionNameKeyPath:@"sortDate"
                                                                           cacheName:nil];
...

The Problem: On any day, any records entered does not show up in the tableview unless I dismiss the tableview then reload it. I get this error in the debugger:

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 (3) 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 (0 inserted, 0 deleted). with userInfo (null)

The same thing happens if I delete the last item in any section.

I am stumped on how to get the view to handle the addition or removal of sections when needed.

Thanks in advance...

Upvotes: 1

Views: 76

Answers (1)

BBruce
BBruce

Reputation: 265

Found it... Needed to implement this method:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

switch(type) {
    case NSFetchedResultsChangeInsert:
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}

Upvotes: 1

Related Questions