NSFetchedResultsController部分索引转到错误的行

时间:2011-07-11 03:36:09

标签: xcode uitableview nsfetchedresultscontroller nsfetchrequest

我有一个FRC的部分。我已经实现了部分索引(就像其他6个视图一样),它显示了;但是,当我点击其中一个索引时,它会进入中间并停止。我的FRC和sectionIndex方法如下。

我逐步调试了调试器中的代码,sectionForSectionIndexTitle返回正确的索引(如果重要,则为15),但它只是停在“I”处。

有什么想法吗?

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

编辑:

return index - 1; <-- This is wrong

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

}

1 个答案:

答案 0 :(得分:2)

D'哦!好吧,我希望这可以节省其他人几个小时。 。 。

in

sectionForSectionIndexTitle

而不是

return index - 1;

应该是

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

我不确定为什么它在其他人中起作用,但我也在改变它们。