1

セクション付きのFRCがあります。セクションインデックスを実装しました(それが機能する他の6つのビューと同じように)。ただし、インデックスの1つをタップすると、中央に移動して停止します。私の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]; 

}
4

1 に答える 1

2

D'oh!さて、これで他の誰かが数時間を節約できることを願っています。。。

sectionForSectionIndexTitle

それ以外の

return index - 1;

あるべきだった

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

他の人でなぜそれが機能するのかはわかりませんが、他の人でも変更しています。

于 2011-07-12T03:29:31.703 に答える