使用NSFetchedResult控制器和每个月的部分标题

可能重复:
使用fetchedresultcontroller为每个月设置一个不同的部分

我试图将我的UITableview分成每个月的部分。 我开始使用苹果的DateSectionTitles例子。 首先,我在我的coredata中添加了一个属性“sectionIdentifier”。 然后在我的managedobject子类中,我添加了这个方法。

- (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; } 

这是我在我的titleForHeaderSection

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> 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; } 

对于我的rowsInSection数量,我这样做。

 id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; NSInteger count = [sectionInfo numberOfObjects]; return count; 

而这对于我的NumberOfSectionsInTableview

 NSInteger count = [[self.fetchedResultsController sections] count]; return count; 

任何人都可以帮忙吗?

提前致谢!

出了什么问题

首先它不打印我的日志在NSLog(@“tmp:%@”,tmp); 而且我得到以下错误:

 **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)' 

编辑这里是我设置我的NSFetchresultcontroller

 - (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; 

我认为这一定是

 sectionNameKeyPath:@"sectionIdentifier" 

代替

 sectionNameKeyPath:@"Kalender.sectionIdentifier" 

在取得结果控制器的build设。