7

I have a NSFetchedResultsController which is fetching objects from a NSManagedObjectContext. I'm using the results to populate a UITableView.

I'm filtering with these two sort descriptors.

NSSortDescriptor *lastOpened = 
    [[NSSortDescriptor alloc] initWithKey:@"lastOpened" ascending:NO];

NSSortDescriptor *titleDescriptor = 
    [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];

And when I create the NSFetchedResultsController, I sort the sections via sectionNameKeyPath:@"lastOpened".

Right now my sections display the standard format like 2009-07-02 20:51:27 -0400 and since no two can be opened at the same time, they are all unique. I need them to cover range of date/times, such as an entire day, and be in a human-readable form. Something like Thursday, July 2.

Thanks!


Edit:

This is all inside a UITableViewController. Here's some more code.

- (NSString *)tableView:(UITableView *)tableView  titleForHeaderInSection:(NSInteger)section {
    // Display the dates as section headings.
    return [[[fetchedResultsController sections] objectAtIndex:section] name];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}
Michael Grinich
  • 4,770
  • 8
  • 29
  • 30
  • Are you using bindings to populate the table or have you written a datasource for the UITableView? – Jeff Hellman Jul 03 '09 at 02:13
  • The questioner is developing for the iPhone, so it can't be Bindings, as that doesn't exist in Cocoa Touch. http://developer.apple.com/IPhone/library/documentation/Miscellaneous/Conceptual/iPhoneOSTechOverview/PortingfromCocoa/PortingfromCocoa.html – Peter Hosey Jul 03 '09 at 03:20
  • You should be filtering out the results by setting `NSPredicates` to your Fetched results controller, btw. You can still sort more than one way, but if you only want to show certain objects where certain "properties are true" (ie a predicate is met) then use NSPredicates. – jbrennan Sep 12 '09 at 20:12

2 Answers2

12

I ended up adding a new property day to my NSManagedObject subclass to get a formatted date string.

@property (nonatomic, readonly) NSString * day;
@property (nonatomic, retain) NSDateFormatter * dateFormatter

Sythesize the dateFomatter. @synthesize dateFormatter;

I initialize the date formatter in awakeFromFetch and awakeFromInsert.

self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];

The accessor for day.

- (NSString *)day {
  NSDate *date = self.startDate;
  return [self.dateFormatter stringFromDate:date];
}

I then set my section name keypath to look at day; you shouldn't need to make any changes to your sort descriptors.

Will Johnston
  • 864
  • 4
  • 18
0

I haven't had a chance to dig into that area of the SDK yet (my work is with C#, so the iPhone stuff is in my hobby time), but from what I can tell you would want to use the other init, specifically initWithKey:ascending:selector: and for the selector you could pass a selector to your compare method that would only compare on the Year/Month/Day, ignoring time.

Here's the url for reference: iPhone Library Documentation

Hope this helps!

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49