Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6473245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:26:36+00:00 2026-05-25T06:26:36+00:00

i generate a tableview using core-data and a NSFetchedResultsController with sectionNameKeyPath . My Core-Data

  • 0

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 release];
[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)

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:26:37+00:00Added an answer on May 25, 2026 at 6:26 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I generate some png charts and excel files using a mysql database. I display
I have this idea that using SQL VIEWS to abstract simple database computations (such
I have a tableview with data, that i post to the app engine database.
I generate an excel file using XML format outlined on stackoverflow in the following
We generate pdf files with data regarding monthly financial balance of tens of thousands
I generate csv files with fputcsv and it works just fine, but when I
I wanted to generate one fix view using interface builder, but the size of
I am basically trying to reload the UITableView data using blocks when the data
By generate, I mean auto-generation of the code necessary for a particular selected (set
We generate web pages that should always be printed in landscape mode. Web browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.