The documentation for NSFetchedResultsController contains sample code that works just fine.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = ; NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
Set the sortDescriptors of the query to fetch so that the results are sorted using the article. Set sectionKeyPath to "articleSection" so that NSFetchedResultsController creates sections for you. Something like that:
NSFetchRequest *request = [[NSFetchRequest alloc] init]; request.entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];; request.fetchBatchSize = 20; // sort by "articleSection" NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"articleSection" ascending:YES]; request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil];; // create nsfrc with "articleSection" as sectionNameKeyPath NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"articleSection" cacheName:@"MyFRCCache"]; frc.delegate = self; NSError *error = nil; if (![frc performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } self.fetchedResultsController = frc;
source share