1

我有一個帶有節的FRC。我已經實現了部分索引(就像其他6個視圖一樣),並且它顯示了;但是,當我點擊其中一個索引時,它會進入中間並停止。下面是我的FRC和sectionIndex方法。NSFetchedResultsController節索引進入錯誤的行

我在調試器中遍歷了代碼,sectionForSectionIndexTitle返回正確的索引(如果它很重要的話),但它只是停在「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]; 

} 

回答

2

D'哦!那麼我希望這可以節省一些人幾個小時。 。 。

sectionForSectionIndexTitle 

,而不是

return index - 1; 

它應該是

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

我不知道爲什麼它工作在別人,但我改變了它在那些也是如此。

+0

只是一個任何人遇到這個線程的FYI,它似乎'索引-1'是不正確的,因爲它引發了一個異常。在我的情況下,使用簡單的'index'工作正確。 OP必須有一些奇怪的事情發生。 –

+0

簡單地嘗試後 '[self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];'失敗了,我試着...'atIndex:(index - 1)'which worked。我很緊張,這可能是數據依賴。索引中的第一項是'1'(公司喜歡稱自己爲「第一無論」 – johnlholden

相關問題