0

I'm quite the newbie at Core Data, so please bear with me. ;)

In my Data Model, I have a single entity called "Item", with 2 attributes, "Section" and "Name." I already have the UITableView (my main view) populated using by Name, but I want to divide the TableView into 3 different sections, determined by attribute "section." I'm thinking that the "section" attribute should be an integer, so 0 would be the first section, 1 would be the second and 2 would be the third.

I really am not sure how to divide the TableView into the sections. I'm using NSFetchedResultsController. Here's the current code.

- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *dbEntityName = @"Item"; // Put your entity name here

// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:dbEntityName];

// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Item.name = what"];

// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
                                                                                 ascending:YES
                                                                                  selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                    managedObjectContext:self.managedObjectContext
                                                                      sectionNameKeyPath:nil
                                                                               cacheName:nil];
[self performFetch];
}

Also, I was wondering if the Fetch Request template inside the data model in Xcode would help out at all. I see how to create the fetch request, but not how to make it do anything with what it fetches (ex: put the fetched data into a certain TableView section.)

If anyone could help out, it would be greatly appreciated.

mhbdr
  • 753
  • 2
  • 10
  • 26

1 Answers1

3

You need to choose a sectionNameKeyPath when creating the NSfetchedResultsController. That should point to a property in your model objects that will be the same for every item in the same section.

To display section headers, make sure you implement all tableView dataSource methods as in the example in NSfetchedResultsController documentation.

LocoMike
  • 5,626
  • 5
  • 30
  • 43
  • Thanks so much for responding! :) Any chance you could provide an example for me? I'm not exactly sure what to put down for the sectionNameKeyPath. – mhbdr May 29 '12 at 02:53
  • In your case, it would be "section" for the section name key path. It's exactly what it sounds like - a key path that leads to the section name. Integers probably aren't the best choice though as this won't make for very attractive section headers. – jrturton May 29 '12 at 05:50
  • Hmm, ok, thanks. Would it just be sectionNameKeyPath:@"section"? That seems easy. Would using a string be better for the headers? – mhbdr May 29 '12 at 15:02