Can anyone tell me why the below code works...
Any this code does not…
The error I am getting is "[NSDefaultSectionsInfo myDate] unrecognized selector"
"myDate" is an attribute, of NSDate type. The entity is patient. What I am trying to do is create an entity (patient), with attributes myDate and name. I want to sort sections based on date. Entries on the same date will fall under the same section.
Any help is appreciated.
Adam
The below code compiles without errors, but the sections do not appear.
Any thoughts? Thanks.
The below code works to the extent that I get no errors. However, I also don't get any section titles, either, or any sections.
I guess the problem must be in my fetchedResultsController method, which I list below…
Again any help is appreciated.
I used NSLog in the following way…
The result is below...
I think the problem is that there is a time associated with each of the dates that I create using a date picker, even though I specify in the IB that it is date (not time). Therefore I'm not sure that names with the same date will appear in the same section, like I would like.
The other problem is that I don't seem to convert from the above strings, into a format that will appear on the secion header. Again below is the code that I used…
Again thanks for the help. Adam
Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
return [[[fetchedResultsController sections] objectAtIndex:section] name];
}
Any this code does not…
Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
Patient*patient = [[fetchedResultsController sections] objectAtIndex:section];
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
return [dateFormatter stringFromDate:[patient myDate]];
}
The error I am getting is "[NSDefaultSectionsInfo myDate] unrecognized selector"
"myDate" is an attribute, of NSDate type. The entity is patient. What I am trying to do is create an entity (patient), with attributes myDate and name. I want to sort sections based on date. Entries on the same date will fall under the same section.
Any help is appreciated.
Adam
The below code compiles without errors, but the sections do not appear.
Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
NSString*someDate;
someDate = [[[fetchedResultsController sections] objectAtIndex:section]name];
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSDate *secondDate = [dateFormatter dateFromString:someDate];
return [dateFormatter stringFromDate:secondDate];
}
Any thoughts? Thanks.
The below code works to the extent that I get no errors. However, I also don't get any section titles, either, or any sections.
Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
NSString*someDate;
someDate = [[[fetchedResultsController sections] objectAtIndex:section]name];
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *secondDate = [dateFormatter dateFromString:someDate];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSString*secondString;
secondString=[dateFormatter stringFromDate:secondDate];
return secondString;
}
I guess the problem must be in my fetchedResultsController method, which I list below…
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 *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:@"myDate" cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management.
[aFetchedResultsController release];
[fetchRequest release];
[authorDescriptor release];
[titleDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
Again any help is appreciated.
I used NSLog in the following way…
Code:
NSString*someDate;
someDate = [[[fetchedResultsController sections] objectAtIndex:section]name];
NSLog(@"%@",someDate);
The result is below...
Code:
2010-11-28 16:20:14.129 Patients[347:207] 2018-12-28 14:00:31 -0500
2010-11-28 16:20:14.134 Patients[347:207] 2010-04-04 08:43:14 -0400
2010-11-28 16:20:14.135 Patients[347:207] 2010-06-29 21:44:15 -0400
2010-11-28 16:20:14.137 Patients[347:207] 2010-10-28 14:01:53 -0400
2010-11-28 16:20:14.139 Patients[347:207] 2010-11-28 09:05:14 -0500
2010-11-28 16:20:14.141 Patients[347:207] 2010-11-29 21:43:30 -0500
I think the problem is that there is a time associated with each of the dates that I create using a date picker, even though I specify in the IB that it is date (not time). Therefore I'm not sure that names with the same date will appear in the same section, like I would like.
The other problem is that I don't seem to convert from the above strings, into a format that will appear on the secion header. Again below is the code that I used…
Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
NSString*someDate;
someDate = [[[fetchedResultsController sections] objectAtIndex:section]name];
NSLog(@"%@",someDate);
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *secondDate = [dateFormatter dateFromString:someDate];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSString*secondString;
secondString=[dateFormatter stringFromDate:secondDate];
return secondString;
}
Again thanks for the help. Adam
Last edited: