开发者

Core Data sectioned TableView ordered wrong

i generate a tableview using core-data and a NSFetchedResultsController with sectionNameKeyPath. My Core-Data entities look fine, also in the SQL-Database the Data looks good.

The Entity is called "Cast" and looks like this:

Cast
  -> job
  -> department // the attribute i want the sections from

i generate my NSFetchedResultsController like this

// 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 r开发者_StackOverflowelease];
[theFetchedResultsController release];

// Fetch Casts
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
    // Update to handle the error appropriately.
    XLog("Unresolved error %@, %@", error, [error userInfo]);
}

But the result is the following (i added the "department" attribute into the detail attribute to show the problem)

Core Data sectioned TableView ordered wrong

as you can see. the sections are generated properly, but then the single entities are completely random inserted into the sections.

can anybody see a bug in my code?

here is the rest of the code that is related to the 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;

}

thanks for all hints. please leave a comment if something is unclear.


You should sort by department first.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜