views:

237

answers:

2

I am playing around with the coredatabooks source code example from the apple website, or here. I am trying to set the books copyright date attribute value to replace the author as the tableview section header, and I need the date value to be static, meaning I don't need the time, otherwise all of the date values are different, and no two books objects with the same month day and year copyright date lineup under the same date because the time portion of the date value is changing...

Here is the code from my RootViewController.m file that formats the datepicker date value for display in the section header:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *rawDateStr = [[[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:@"EEEE MMMM d"];
NSString *formattedDateStr = [formatter stringFromDate:date];

return formattedDateStr;

}

That outputs just fine, but it isnt letting me group books with the same copyright month day and year in the same section because again (I am assuming) the date value has the time as well. The actual saving of the date value from the datepicker is done in the EditingViewController.m file as follows:

 - (IBAction)save {

// Pass current value to the edited object, then pop.
if (editingDate) {
    [editedObject setValue:datePicker.date forKey:editedFieldKey];
}

The save method currently takes the date as-is from the datepicker, so how do I modify that code to first copy the datepicker's date to a local variable and blank out the time in there and then use that value in the setValue statement so it groups correctly? PLEASE HELP! Thanks

A: 

Sorry, this isn't really an answer per say, but here is the link to the answer that fixed my issue. Thanks for everyones help.

http://stackoverflow.com/questions/1827760/date-formatting-inside-the-uitableview-section-heading-please-help

Steve
You don't need to do all that explicit casting.
Dave DeLong
@Wes: This should have been a comment linking to the other answer. Even better, you can delete your post.
Sinan Ünür
+1  A: 

Since date's inherently include time, you're going to have to modify the NSDate object itself by converting it into its NSDateComponents, blanking out the HH:mm:ss, and then converting those date components back into an NSDate.

For example:

NSDate * aDate = [NSDate date];
NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:aDate];
NSDate * convertedDate = [[NSCalendar currentCalendar] dateFromComponents:components];

If you do this, you'll lose things like timezone offset, so you might want to consider the side effects of that.

The nice thing about this method is that it works regardless of how the string is formatted (which is a fragile thing to rely on).

Dave DeLong