I did a previous post where I had difficulty with the "titleForHeaderInSection" method, with date formatting. I appreciate the help regarding this problem. This is the same program. My current problem is that each of these dates is stamped with a time, even if the date picker is in date-only format. I did some looking online and came up with the methods below. You create a NSString attribute. "myDate" is an attribute, type is date. It is used to order the sections, as seen in the NSFetchedResultsController method.
I got the other code from several sites online. I created myFormattedDate as an attribute, type string. It is then used as the sectionNameKeyPath. The idea is to just have a date to partition the sections, rather than a timestamp. So same dates go into the same section. However, when I do NSLog(@"%@",myFormattedDate), it comes back null.
Thanks in advance for any help.
I tried something else, but still not working...
The program I am working on is a modified version of "CoreDataBooks", taken from Apple iPhone sample code. In "CoreDataBooks" there is a list of authors, some of whom have multiple books. A navigation controller is set up, with the sections divided up by author name. In each section, there is a list of books for each author.
So I took this code and am trying to make a list of names, arranged by section. My entity is called "Patient". Each entity has attributes myDate and myName, as well as a few others. I want to arrange sections by myDate, with a list of names in each section, all of which have the same date.
I am having difficulty with the NSFetchedResultsController method. When the dates are created using a date picker, they all have a timestamp. This timestamp makes them different, and they will be placed into separate sections if you sort by myDate in the sectionNameKeyPath method.
I did fairly extensive searching online, and came up with the solution below. You create another transient attribute, an NSString, and then use it to sort the sections. This is done by creating a date formatter, which is shown below. I include my setter for my NSString, as well as the NSFetchedResultsController method.
Any help will be greatly appreciated.
The current error I am getting is "the entity Patient is not key value coding-compliant for the key "transactionDay""
Thanks, again.
...
I think the answer to this problem is handled in the sample code "DateSectionTitles". Will take me a little while to go through this code and figure it out.
I got the other code from several sites online. I created myFormattedDate as an attribute, type string. It is then used as the sectionNameKeyPath. The idea is to just have a date to partition the sections, rather than a timestamp. So same dates go into the same section. However, when I do NSLog(@"%@",myFormattedDate), it comes back null.
Code:
-(void)awakeFromFetch
{
secondDateFormatter=[[[NSDateFormatter alloc]init]autorelease];
[secondDateFormatter setDateFormat:@"MM/dd/yyyy"];
}
-(NSString*)myFormattedDate
{
NSDate*date=[self valueForKey:@"myDate"];
return [self.secondDateFormatter stringFromDate:date];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
PatientsAppDelegate*appDelegate=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext*managedObjectContext=appDelegate.managedObjectContext;
// Create and configure a fetch request with the Book entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Patient" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"myDate" ascending:YES];
NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"myName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, titleDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"myFormattedDate" cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management.
[aFetchedResultsController release];
[fetchRequest release];
[authorDescriptor release];
[titleDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
Thanks in advance for any help.
I tried something else, but still not working...
The program I am working on is a modified version of "CoreDataBooks", taken from Apple iPhone sample code. In "CoreDataBooks" there is a list of authors, some of whom have multiple books. A navigation controller is set up, with the sections divided up by author name. In each section, there is a list of books for each author.
So I took this code and am trying to make a list of names, arranged by section. My entity is called "Patient". Each entity has attributes myDate and myName, as well as a few others. I want to arrange sections by myDate, with a list of names in each section, all of which have the same date.
I am having difficulty with the NSFetchedResultsController method. When the dates are created using a date picker, they all have a timestamp. This timestamp makes them different, and they will be placed into separate sections if you sort by myDate in the sectionNameKeyPath method.
I did fairly extensive searching online, and came up with the solution below. You create another transient attribute, an NSString, and then use it to sort the sections. This is done by creating a date formatter, which is shown below. I include my setter for my NSString, as well as the NSFetchedResultsController method.
Any help will be greatly appreciated.
Code:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
PatientsAppDelegate*appDelegate=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext*managedObjectContext=appDelegate.managedObjectContext;
// Create and configure a fetch request with the Book entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Patient" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *dateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"myDate" ascending:YES];
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"myName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dateDescriptor, nameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// NSLog(@"%@",myFormattedDate);
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"transactionDay"
cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management.
[aFetchedResultsController release];
[fetchRequest release];
[dateDescriptor release];
[nameDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
- (NSString *)transactionDay{
[self willAccessValueForKey:@"transactionDay"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM dd,yyyy"];
NSString*someString=[dateFormatter stringFromDate:[self valueForKey:@"myDate"]];
[self didAccessValueForKey:@"transactionDay"];
return someString;
}
The current error I am getting is "the entity Patient is not key value coding-compliant for the key "transactionDay""
Thanks, again.
...
I think the answer to this problem is handled in the sample code "DateSectionTitles". Will take me a little while to go through this code and figure it out.
Last edited: