核心数据部分TableView命令错误

时间:2011-09-05 08:54:21

标签: iphone objective-c ios core-data nsfetchedresultscontroller

我使用核心数据生成一个tableview,使用NSFetchedResultsController生成一个sectionNameKeyPath。我的核心数据实体看起来很好,在SQL数据库中看起来也很好。

实体名为“Cast”,如下所示:

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属性中以显示问题)

enter image description here

你可以看到

这些部分是正确生成的,但随后单个实体被完全随机地插入到部分中。

任何人都可以在我的代码中看到错误吗?

这是与cell / section stuff相关的其余代码

- (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;

}

感谢所有提示。 如果不清楚,请发表评论。

1 个答案:

答案 0 :(得分:5)

您应该先按department排序。

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];