【发布时间】:2014-02-22 22:07:35
【问题描述】:
出于某种原因,无论数据模型是什么样的,我的方法调用 -numberOfSectionsInTableView 总是返回一个部分。
这是我的 TableVIewController.m 中的一些 sn-ps 代码。如果您需要,请随时询问更多信息
self.sortDesciptor = [[NSSortDescriptor alloc] initWithKey:@"weaponData" ascending:YES];
self.sortDescriptors = [NSArray arrayWithObjects:self.sortDesciptor, nil];
self.sortSectionKey = @"weaponData";
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"number of sections:%lu",(unsigned long)[[_fetchedResultsController sections] count]);
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSLog(@"%lu", (unsigned long)[sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [[[sectionInfo objects] objectAtIndex:section] weaponData];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
sTCAppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =[appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TargetData" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:0];
if (self.sortMethod == 0){
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
}
else{
[fetchRequest setSortDescriptors:self.sortDescriptors];
}
NSFetchedResultsController *aFetchedRequestsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:self.sortSectionKey cacheName:nil];
aFetchedRequestsController.delegate = self;
self.fetchedResultsController = aFetchedRequestsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
在我的代码中的这个例子中,想想如果我的“weaponData”实体中有两个“weaponData”对象会发生什么。假设第一个被命名为狙击步枪,第二个被命名为突击步枪。这应该创建两个部分,一个名为 Sniper Rifle 和一个名为 Assault Rifle。实际发生的是,我只得到 1 个部分,它是根据首先进入对象上下文的任何对象命名的。
有什么办法解决这个问题吗?
【问题讨论】:
-
sectionKey = 用于确定武器类型的密钥?
-
是的。 sortSectionKey 等于 sortDescriptorKey。这可以显示,因为该应用程序至少会正确显示一个带有武器名称的部分。由于某种原因,它的作用不超过 1
-
武器数据是包含武器名称的属性吗?所以对象 1 的武器数据 = 狙击步枪,对象 2 的武器数据 = 突击步枪?
-
是的,没错
-
加载获取结果控制器时,即在线: NSFetchedResultsController *aFetchedRequestsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:self.sortSectionKey cacheName:nil]; self.sortSectionKey 名称是您想要的字符串吗?
标签: ios uitableview core-data nsfetchedresultscontroller sections