Search code examples
xcodeuitableviewnsfetchedresultscontrollernsfetchrequest

NSFetchedResultsController Section Index is going to the wrong row


I've got a FRC with sections. I've implemented the section index (just like in 6 other views where it works), and it shows; however, when I tap on one of the indexes it goes to the middle and stops. My FRC and sectionIndex methods are below.

I stepped through the code in the debugger, sectionForSectionIndexTitle returns the proper index (15 if it matters), but it just stops at "I".

Any ideas?

- (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)aPredicate {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
//[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortByName  = [[NSSortDescriptor alloc] initWithKey:@"Category" ascending:YES];
NSSortDescriptor *sortByPG = [[NSSortDescriptor alloc]initWithKey:@"ProductGroup" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortByName, sortByPG, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortByName release];
[sortByPG release];
[sortDescriptors release];
NSString *cacheName = nil; // @"Root";
if(!aPredicate){
    //aPredicate = [NSPredicate predicateWithFormat:@"Category <> %@", @"EQUIPMENT"];
}
[fetchRequest setPredicate:aPredicate];  
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"Category" cacheName:cacheName];


aFetchedResultsController.delegate = self;

[fetchRequest release];

NSError *anyError = nil;


if(![aFetchedResultsController performFetch:&anyError]){
    NSLog(@"Error fetching: %@", anyError);
}
return [aFetchedResultsController autorelease];  

}

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
 {
NSMutableArray *sectionTitles = [[[NSMutableArray alloc] init] autorelease];
[sectionTitles addObject:UITableViewIndexSearch];
[sectionTitles addObjectsFromArray:[self.fetchedResultsController sectionIndexTitles]];
return sectionTitles;
//return [self.fetchedResultsController sectionIndexTitles];
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{

if (index == 0) {
    [tableView setContentOffset:CGPointZero animated:NO];
    return NSNotFound;
}
//NSInteger indexToGoTo =  [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];

EDIT:

return index - 1; <-- This is wrong

//This is right!
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index - 1]; 

}

Solution

  • D'oh! Well I hope this saves somebody else a couple of hours. . .

    in

    sectionForSectionIndexTitle
    

    instead of

    return index - 1;
    

    it should have been

    return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index - 1];
    

    I'm not sure why it works in the others, but I'm changing it in those as well.