Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top