0

In my app, I have a UITableView that is populated by Core Data, currently a string called "name." I want to be able to divide this tableview into 3 sections, which could be named 0, 1 and 2, or 1, 2 and 3. The name doesn't really matter. I currently really don't know how to achieve this. I have a new attribute in my data model called "term" which is what I want the sections to be based off of. Is it best for this to be an integer?

I'm kind of a newbie at iOS development, so if anyone knows how do this, I would greatly appreciate it .

Here's my current code that sorts that populates the tableview with the "name" attribute.

- (void)setupFetchedResultsController
{
    //1 - Entity
    NSString *entityName = @"Task"; // Put your entity name here
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    //2 Request
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

    // 3 - Filter
    //request.predicate = [NSPredicate predicateWithFormat:@"Task.name = name"];

    // 4 - Sort
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
}

Thanks everyone!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
mhbdr
  • 753
  • 2
  • 10
  • 26

1 Answers1

0

I don't understand if you use a NSFetchedResultsController or not.

In the first case (you use it) you can take advantage of it setting term as the attribute for dividing in section. When you create a NSFetchedResultsController with – initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName: you can pass@"term" as a parameter for sectionNameKeyPath.

In the second case, I really suggest to deal with a NSFetchedResultsControllers when dealing with UITableViews. Setting up a NSFetchedResultsController is really sample since you need to create it and pass it a NSFetchRequest. The one you created is ok.

For further info I suggest to read core-data-tutorial-how-to-use-nsfetchedresultscontroller and also NSFetchedResultsController class reference.

Hope it helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190