Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not sure if I understand your question completely, but I do something similar in my app and here's how I get it to work:</p> <p>Firstly, the fetchedResultsController method, where I set the sort descriptions and predicates based on what I am trying to do. In this case I want to sort movie titles by release date THEN by name. Then with my predicate I grab entities of a specific 'type' and within a certain 'releaseDate' range.</p> <p>In my fetchresultscontroller definition, you set the sectionNameKeyPath to "releaseDate" so my section headers will be based on a date.</p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController { if (fetchedResultsController_ != nil) { return fetchedResultsController_; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSSortDescriptor *sortByReleaseDate = [[NSSortDescriptor alloc] initWithKey:@"releaseDate" ascending:NO]; NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByReleaseDate,sortByName, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortByName release]; [sortByReleaseDate release]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(type == 'Movies') AND (releaseDate &lt;= %@) AND (releaseDate &gt;= %@)", [NSDate date], [NSDate dateWithTimeIntervalSinceNow:kOneDayTimeInterval*-30]]; [fetchRequest setPredicate:predicate]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movie" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"releaseDate" cacheName:nil]; ...// Perform and return fetch here, error handling etc... return fetchedResultsController_; } </code></pre> <p>Then in my table view data source delegate methods, I return the actual title for each header after transforming my NSDate into NSString (remember you have to return NSString for a tableview header title.</p> <pre><code>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name]; //convert default date string to NSDate... NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZ"]; NSDate *date = [formatter dateFromString:rawDateStr]; //convert NSDate to format we want... [formatter setDateFormat:@"d MMMM yyyy"]; NSString *formattedDateStr = [formatter stringFromDate:date]; return formattedDateStr; } </code></pre> <p>So if I wanted to change the way my data is being displayed to be organised by titleName for instance, I'd change my fetchedResultsController object to:</p> <pre><code>NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"titleName" cacheName:nil]; </code></pre> <p>And modify my tableview:titleForHeaderInSection: data source method to simply return the titleName (which is already a string):</p> <pre><code> - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[[self.fetchedResultsController sections] objectAtIndex:section] name]; } </code></pre> <p>I hope this helps you find a solution to your specific problem.</p> <p>Cheers, Rog</p>
 

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