NSFetchedResultsController 创建部分

发布于 2024-11-30 01:53:39 字数 2198 浏览 4 评论 0原文

当使用 NSFetchedResultsController 为 UITableViewController 创建节标题时, fetchedResultsController.sections 为每个项目而不是每个节都有一个对象。

//Set up the request
NSManagedObjectContext *context = self.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:[NSEntityDescription entityForName:@"Person" 
                                    inManagedObjectContext:context]];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = nil;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" 
                                             ascending:YES];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[sortDescriptor release]; sortDescriptor = nil;

[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;

//setup fetch results controller
NSFetchedResultsController *controller = nil;
controller = [[NSFetchedResultsController alloc] 
              initWithFetchRequest:fetchRequest 
              managedObjectContext:context 
                sectionNameKeyPath:@"firstName" 
                         cacheName:@"PersonCache"];

__fetchedResultsController = controller;

[fetchRequest release]; fetchRequest = nil;

//IMPORTANT: Delete cache before changing predicate
[NSFetchedResultsController deleteCacheWithName:nil]; 
NSError *error = nil;
if (![controller performFetch:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
} else if ([[controller fetchedObjects] count] == 0){
    [self retrievePeoples];
}

NSLog(@"result count: %i", [[controller fetchedObjects] count]);
NSLog(@"section count: %i", [[controller sections] count]);
NSLog(@"sectionIndexTitles count: %i", [[controller sectionIndexTitles] count]);

这将返回:

result count: 18
section count: 18
sectionIndexTitles count: 13

部分计数和sectionIndexTitles 计数不应该匹配吗?当调用 numberOfSectionsInTableView: 和 tableView:numberOfRowsInSection: 方法时,我应该能够查看 fetchedResultsController.section 的计数,而无需任何额外的排序。

如何正确设置 NSFetchResultsController 以使sections数组中的每个对象适用于每个节而不是所有对象?

When using the NSFetchedResultsController to create section headers for the UITableViewController the fetchedResultsController.sections has an object for each item not each section.

//Set up the request
NSManagedObjectContext *context = self.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:[NSEntityDescription entityForName:@"Person" 
                                    inManagedObjectContext:context]];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = nil;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" 
                                             ascending:YES];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[sortDescriptor release]; sortDescriptor = nil;

[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;

//setup fetch results controller
NSFetchedResultsController *controller = nil;
controller = [[NSFetchedResultsController alloc] 
              initWithFetchRequest:fetchRequest 
              managedObjectContext:context 
                sectionNameKeyPath:@"firstName" 
                         cacheName:@"PersonCache"];

__fetchedResultsController = controller;

[fetchRequest release]; fetchRequest = nil;

//IMPORTANT: Delete cache before changing predicate
[NSFetchedResultsController deleteCacheWithName:nil]; 
NSError *error = nil;
if (![controller performFetch:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
} else if ([[controller fetchedObjects] count] == 0){
    [self retrievePeoples];
}

NSLog(@"result count: %i", [[controller fetchedObjects] count]);
NSLog(@"section count: %i", [[controller sections] count]);
NSLog(@"sectionIndexTitles count: %i", [[controller sectionIndexTitles] count]);

This returns:

result count: 18
section count: 18
sectionIndexTitles count: 13

Shouldn't the section count and the sectionIndexTitles count match? When the numberOfSectionsInTableView: and tableView:numberOfRowsInSection: methods are called I should just be able to look to the fetchedResultsController.section for a count without any additional sorting.

How do I setup the NSFetchResultsController properly to have each object in the sections array be for per section and not for all objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

狼性发作 2024-12-07 01:53:39

菲利普米尔斯在另一个论坛上回答了这个问题。问题是我使用整个名字来创建部分(而不仅仅是第一个字母)。解决方法是更新实体以在更新或更改时创建部分标题。 Apple 的 DateSectionTitles 有一个操作示例。

This was answered by Phillip Mills on another forum. The problem was I was using the entire firstName to create sections (not just the first letter). The fix is to update the entity to create the a section title whenever updated or changed. Apple's DateSectionTitles has a sample of what to do.

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