NSFetchedResultsController 部分索引指向错误的行

发布于 2024-11-19 18:01:50 字数 2564 浏览 7 评论 0原文

我有一个带有部分的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-11-26 18:01:50

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

sectionForSectionIndexTitle

不是

return index - 1;

它应该是

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文