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

  • SEARCH
  • Home
  • 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 9226319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:48:03+00:00 2026-06-18T04:48:03+00:00

I am having trouble getting sections with a NSFetchedResultsController working. I have an entity,

  • 0

I am having trouble getting sections with a NSFetchedResultsController working. I have an entity, say ‘Employee’ and a string attribute, let’s say ‘name’. Now I want to show all employee’s names in a UITableView using a NSFetchedResultsController… no problem, heres my Code:

if (_fetchedResultsController == nil) {

    NSManagedObjectContext *moc = [appDelegate managedObjectContext];

    NSFetchRequest *request = [NSFetchRequest new];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

    [request setEntity:entity];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:@"name" cacheName:@"root"];

    NSError *error;
    [_fetchedResultsController performFetch:&error];

    if (error != nil) {
        NSLog(@"Error: %@", error.localizedDescription);
    }
}

But the NSFetchedResultsController creates a section for each entity. So when I have 200 employees, it creates 200 sections. Why?

And how do I implement those methords properly:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
   return [[_fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     // Return the number of rows in the section.
     return [[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    cell.textLabel.text = [[_fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"name"];

    return cell;
}

May I do understand the whole concept wrong? Where is the difference between [_fetchedResultsController sections] and [_fetchedResultsController sectionIndexTitles]?

I hope you can help me and thanks in advance!

EDIT: I forget to tell you the most important thing: I want to have sections separated by the first letter of the ‘name’ attribute. (Like the Music-APP).

Nick

  • 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-06-18T04:48:04+00:00Added an answer on June 18, 2026 at 4:48 am

    Pass nil as your sectionNameKeyPath when initializing your NSFetchedResultsController.

    If you pass name, you basically say “please create one section for each name”. When passing nil, you tell it to create only a single section.

    You tableview method implementations look right to me.

    [_fetchedResultsController sections] gives you an array with objects that you can ask for things like the number of objects in a section. In contrast, [_fetchedResultsController sectionIndexTitles] is mostly so you can tell the NSFetchedResultsController which section titles to use (i.e you would set this to an array with one string for each section). In your case, you can just ignore it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im having trouble getting a string (containing a URL) from one activity to another.
I'm having trouble getting Tire working using ElasticSearch with the Bonsai addon on the
I'm having trouble getting the sponsors sections of the following website to center correctly,
I have been having trouble getting my banner image on my blog to be
I'm having trouble getting my head around how to define certain sections of a
I am having trouble getting this code to run using Google Chrome(I have not
I have been having trouble getting my Opencart site to work when moving it
Im having trouble getting my JTable that im using to display either check boxes
I'm having trouble getting the CanExecute method of my command to work property. I've
I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming

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.