title: working with a NSFetchedResult controller and section headers for each month
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12815093/setting-a-different-section-for-each-month-with-fetchedresultcontroller">Setting a different section for each month with fetchedresultcontroller</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to split my <strong>UITableview</strong> into sections for each month. I start working with the <a href="http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html" rel="nofollow noreferrer">DateSectionTitles</a> example of apple. First of all I added in my <strong>coredata</strong> a attribute 'sectionIdentifier'.  Then in my <strong>managedobject</strong> subclass I added this method.</p>
  
  <pre><code>- (NSString *)sectionIdentifier {
  
      // Create and cache the section identifier on demand.
  
      [self willAccessValueForKey:@"sectionIdentifier"];
      NSString *tmp = [self primitiveSectionIdentifier];
      [self didAccessValueForKey:@"sectionIdentifier"];
  
      if (!tmp) {
          /*
           Sections are organized by month and year. Create the section identifier as a string representing the number (year * 1000) + month; this way they will be correctly ordered chronologically regardless of the actual name of the month.
           */
          NSCalendar *calendar = [NSCalendar currentCalendar];
  
          NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[self date]];
          tmp = [NSString stringWithFormat:@"%d", ([components year] * 1000) + [components month]];
            [self setPrimitiveSectionIdentifier:tmp];
      }
      NSLog(@"tmp: %@",tmp);
      return tmp;
  }
  </code></pre>
  
  <p>This is what i do in my titleForHeaderSection</p>
  
  <pre><code>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
          id &lt;NSFetchedResultsSectionInfo&gt; theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
  
  
      /*
       Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
       To display the section title, convert the year and month components to a string representation.
       */
      static NSArray *monthSymbols = nil;
  
      if (!monthSymbols) {
          NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
          [formatter setCalendar:[NSCalendar currentCalendar]];
          monthSymbols = [formatter monthSymbols];
      }
  
      NSInteger numericSection = [[theSection name] integerValue];
  
      NSInteger year = numericSection / 1000;
      NSInteger month = numericSection - (year * 1000);
  
      NSString *titleString = [NSString stringWithFormat:@"%@ %d", [monthSymbols objectAtIndex:month-1], year];
      NSLog(@"titelstring is: %@",titleString);
      return titleString;
  }
  </code></pre>
  
  <p>For my number of rowsInSection I do this.</p>
  
  <pre><code>id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
  
          NSInteger count = [sectionInfo numberOfObjects];
          return count;
  </code></pre>
  
  <p>And this for my NumberOfSectionsInTableview</p>
  
  <pre><code>NSInteger count = [[self.fetchedResultsController sections] count];
          return count;
  </code></pre>
  
  <p>Can anybody help?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>What is going wrong</strong></p>
  
  <p>First off all it don't prints my log in NSLog(@"tmp: %@",tmp); 
  And also I get the following error: </p>
  
  <pre><code>**CoreData**: error: (NSFetchedResultsController) A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
  2012-10-10 10:41:17.475 RacingGenk[15785:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (-1 (or possibly larger)) beyond bounds (12)'
  </code></pre>
  
  <p><strong>EDIT</strong>
  Here is where I set my NSFetchresultcontroller</p>
  
  <pre><code>- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
  {
      NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
      request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];
  
      self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                          managedObjectContext:self.genkDatabase.managedObjectContext
                                                                  sectionNameKeyPath:@"Kalender.sectionIdentifier" cacheName:@"Root"];
      self.fetchedResultsController.delegate = self;
      self.fetchedResultsController = self.fetchedResultsController;
  </code></pre>