Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(I'll write this up assuming you're using an <code>NSFetchedResultsController</code> to drive your tableview. If you're not, I recommend checking it out.)</p> <p>An interesting feature of <code>NSFetchedResultsController</code>'s sectioning abilities: although the property you <em>sort</em> on must be a modeled property (because sqlite does the actual sorting), the property you <em>group the sections with</em> need not be. The only requirement is that the grouping be consistent with the ordering. (i.e., sorting by the sort property will put the objects with matching group properties next to each other.)</p> <p>So just add something like this to your modeled object class:</p> <pre><code>// in interface @property (nonatomic, readonly) NSString *mediumFormattedDate; // in impl -(NSString *)mediumFormattedDate { // this can be fancier if you need a custom format or particular timezone: return [NSDateFormatter localizedStringFromDate:self.date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]; } </code></pre> <p>(no need to mention mediumFormattedDate in the .xcdatamodel at all.)</p> <p>Then go ahead and sort your objects by the date property, but group them by your new property. When you create your <code>NSFetchedResultsController</code>, do so along these lines:</p> <pre><code>NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"MyFancyEntity"]; NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]; [fr setSortDescriptors:[NSArray arrayWithObject:sd]]; NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fr managedObjectContext:myManagedObjectContext sectionNameKeyPath:@"mediumFormattedDate" cacheName:nil]; // then do stuff with frc </code></pre> <p>That's all it takes! I've done this in a few apps to get date grouping and it works well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload