NSFetchedResultsController 创建部分
当使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
菲利普米尔斯在另一个论坛上回答了这个问题。问题是我使用整个名字来创建部分(而不仅仅是第一个字母)。解决方法是更新实体以在更新或更改时创建部分标题。 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.