Indexed NSFetchedResultsController

How can I index the NSFetchedResultsController so that I can implement the AZ index for the table.

I see in the initializer that I can do sectionNameKeyPath , but that just puts the unique objects in its section.

Here is what I have for my NSFetchedResultsController

  - (NSFetchedResultsController *)fetchedResultsController { if (__fetchedResultsController != nil) { return __fetchedResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setFetchBatchSize:20]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"name" cacheName:@"Customer"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; return __fetchedResultsController; } 
+4
source share
4 answers

implement sectionIndexTitlesForTableView: as follows:

 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.fetchedResultsController sectionIndexTitles]; } 

this will give you those indexes on the right side of the table View.

If you want your sections to have names like A, B, C, D, etc., you need to implement a method that returns the first letter of your object.

something like that:

 - (NSString *)firstLetter { [self willAccessValueForKey:@"firstLetter"]; NSString *firstLetter = [[[self name] substringToIndex:1] uppercaseString]; [self didAccessValueForKey:@"firstLetter"]; return firstLetter; } 

This refers to a custom subclass of your coredata object.

Then add a transition attribute named firstLetter to your main data object and replace sectionNameKeyPath in init NSFetchedResultsController with firstLetter

+13
source
Answer

@Harris will show the entire alphabet on the side of the table. Unfortunately, if you then click on one of the letters that actually has a corresponding section, you will come across something like:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Index title at 3 is not equal to 'K'' 
+1
source

If you want to have the letters AZ on the side (even if you do not have partitions for each individual letter), this will do:

 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { index = 0; for(id sectionInfo in [_fetchedResultsController sections]){ if ([[[sectionInfo name] uppercaseString] isEqualToString:title]){ break; } index++; } if (index>=[_fetchedResultsController sections].count){ return -1; } return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index]; } 
0
source

correct version (paragraphs 7.1 to 8.3):

 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { NSInteger section = 0; for (id <NSFetchedResultsSectionInfo> sectionInfo in [_fetchedResultsController sections]) { if ([sectionInfo.indexTitle compare:title] >= 0) { break; } section++; } return section; } 
0
source

Source: https://habr.com/ru/post/1391861/


All Articles