Iphone 核心数据分区表视图排序错误
我使用核心数据和带有sectionNameKeyPath的NSFetchedResultsController生成一个tableview。我的核心数据实体看起来不错,SQL数据库中的数据也不错 该实体称为Cast,如下所示:Iphone 核心数据分区表视图排序错误,iphone,objective-c,ios,core-data,nsfetchedresultscontroller,Iphone,Objective C,Ios,Core Data,Nsfetchedresultscontroller,我使用核心数据和带有sectionNameKeyPath的NSFetchedResultsController生成一个tableview。我的核心数据实体看起来不错,SQL数据库中的数据也不错 该实体称为Cast,如下所示: Cast -> job -> department // the attribute i want the sections from 我像这样生成NSFetchedResultsController // fetch controller NSFet
Cast
-> job
-> department // the attribute i want the sections from
我像这样生成NSFetchedResultsController
// fetch controller
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cast" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"job" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort1, sort2, nil]];
[sort1 release];
[sort2 release];
// Predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"movie == %@", self.movie];
[fetchRequest setPredicate:predicate];
// Generate it
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"department"
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
// Fetch Casts
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
XLog("Unresolved error %@, %@", error, [error userInfo]);
}
但结果如下:我将department属性添加到detail属性中以显示问题
如你所见。正确生成剖面,但随后将单个图元完全随机插入剖面
有人能看到我代码中的错误吗
下面是与单元格/节内容相关的其余代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = nil;
sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
Cast *currentCast = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = currentCast.name;
//cell.detailTextLabel.text = currentCast.job;
// just temporary
cell.detailTextLabel.text = currentCast.department;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *jobTitle = [[[fetchedResultsController sections] objectAtIndex:section] name];
return jobTitle;
}
谢谢你的提示。
如果有不清楚的地方,请留下评论。您应该先按部门排序
你应该先按部门分类
你说得对。我不明白我为什么要这么做,但它是有效的。我觉得触摸可可足够聪明。有没有像我想要的那样想要它的用例?!好吧,我想我明白了。通过这个选项,我可以决定我想要的分区的排序顺序,对吗。这就是为什么我必须这样做。你应该总是先对部分进行排序,正如你所说的那样,你可以设置部分的顺序。然后再按原样进行分类,你是对的。我不明白我为什么要这么做,但它是有效的。我觉得触摸可可足够聪明。有没有像我想要的那样想要它的用例?!好吧,我想我明白了。通过这个选项,我可以决定我想要的分区的排序顺序,对吗。这就是为什么我必须这样做。你应该总是先对部分进行排序,正如你所说的那样,你可以设置部分的顺序。然后做任何子排序,就是这样。
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"department" ascending:YES];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"job" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort1, sort2, sort3, nil]];
[sort1 release];
[sort2 release];
[sort3 release];