I have a fairly working app which just requires a few tweaks and some assistance to get there.
The premise is simple: there is a two-tabbed table view controller and when the user presses the + button in the navigation bar, they're presented with text fields to fill in and a date picker for the date of the event. They press save and it saves to Core Data and then therefore displays that information in the table view. The table view cells contain names and titles, but the section contains the selected dates. The second tab of the table view displays (or should display) only the dates, allowing the user to select which date they want to see the events for.
My "Dates" were recently NSStrings with textFields, before changing that to NSDate and UIDatePicker. *The problem right now is the fact that the section title of the table view is displaying the raw date from the DatePicker, which includes something like 2013-02-01 13:01 0000+. What I want is to change that format to use the NSDateFormatterLong style so it's December 2, 2013 without any time, etc. *
I have worked through a few examples of this type of thing on SO but nothing seems to be working for me.
The model is as follows: - Transaction Entity - Date Entity - Person Entity - Occasion Entity
The Transaction has a relationship with all other entities, including Date (transaction.dateOfEvent).
When I save to Core data for the date, I am calling the method below:
- (IBAction)save:(id)sender
{
NSManagedObjectContext *context = [self managedObjectContext];
Transaction *transaction = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
Date *date = [NSEntityDescription insertNewObjectForEntityForName:@"Date" inManagedObjectContext:context];
date.dateOfEvent = self.datePicker.date;
transaction.dates = date;
}
That works fine and the table view (using NSFetchedResultsControllers) works really well with the chronological dates.
What I want is to change the format of the string from 2013/02/01, etc to December 2, 2013 and I want this same format to be used within the cells of the second tab table view - I'm not sure how to convert that NSDate to an identifiable string for both the section title and the cell textLabel
Any assistance on this would be massively appreciated. Thanks,
EDIT: Including titleForSection method in the TableView
// Following http://stackoverflow.com/questions/4418703/a-nsfetchedresultscontroller-with-date-as-sectionnamekeypath.. I have tried the following
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *rawDateStr = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
// Convert rawDateStr 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;
}
// I have also tried:
NSString * sectionInfo = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [dateFormatter dateFromString:sectionInfo];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"%@", formattedDate);
return formattedDate;
But both are returning the exact same format (and also weirdly hiding most of the section titles leaving just a bit of visibility) but they are showing the exact same format.