NSFetchedResultsController 部分索引指向错误的行
我有一个带有部分的 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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
噢!好吧,我希望这可以为其他人节省几个小时。 。 。
而
不是
它应该是
我不确定为什么它在其他人中起作用,但我也在那些中改变它。
D'oh! Well I hope this saves somebody else a couple of hours. . .
in
instead of
it should have been
I'm not sure why it works in the others, but I'm changing it in those as well.