开发者

Date formatting inside the uitableview section heading, PLEASE HELP

开发者 https://www.devze.com 2022-12-13 03:25 出处:网络
Let me start off by saying Im VERY new to iphone development, but Im trying really hard to learn, so any help any of you professionals out there are willing to share is greatly appreciated! So I have

Let me start off by saying Im VERY new to iphone development, but Im trying really hard to learn, so any help any of you professionals out there are willing to share is greatly appreciated! So I have a question that would be SO awesome if someone could answer for me. I am studying up more one core data and have been using the core data books example from the apple developer website found here. It is a pretty straight forward application, but I am trying to change something and I can't figure out how to do it and it is driving me CRAZY!!! Natively, the app shows the author in the tableview section heading, with the title in the cell. I would like to change that and set the copyright date (one of the attributes of the book) as the section header. Right now, I can get it to show, but it shows the date in this format:

2009-12-01 10:11:31 -0700

But thats not the right format, Id like to use this format:

Tuesday December 1

Probably using this code:

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

[outputFormatter setDateFormat:@"EEEE MMMM d"];

NSString *date = [outputFormatter stringFromDate:[NSDate date]];

but the problem is that the date value is coming in from the datepicker, and I can't figure out (with all this crazy 'key' business) how to format the date value that came from the p开发者_开发百科icker and put it onto the section header. If you have any time to possibly follow the link to the apple wbsite above and poke around until you can answer my dilema, IT WOULD BE SO APPRECIATED!!!! Thank you.

Okay so here is my code I put in the save method:

 // Pass current value to the edited object, then pop.
if (editingDate) {
    NSString *rawDate = (NSString *)datePicker.date;
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = (NSDate *)[outputFormatter dateFromString:rawDate]; 

    [outputFormatter setDateFormat:@"EEEE MMMM d"];
    NSString *formattedDateStr = (NSString *)[outputFormatter stringFromDate:date];

    [editedObject setValue:(NSDate *)formattedDateStr forKey:editedFieldKey];
}

And then for whatever reason, the date just wont save in the app, and the compiler throws this error:

The Debugger has exited with status 0. [Session started at 2009-12-02 09:51:26 -0700.] 2009-12-02 09:51:47.342 CoreDataBooks[17981:20b] * -[__NSCFDate length]: unrecognized selector sent to instance 0x3e79850 2009-12-02 09:51:47.343 CoreDataBooks[17981:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFDate length]: unrecognized selector sent to instance 0x3e79850'

Probably just a quick fix, I am hoping I was at least in the right hemisphere as far as the code goes, thanks again for any help or insight you have time to offer.


You don't have to worry about the key business at least to change the date format here. The copyright property in the book object is automatically changed using the key-business in the EditingViewController.

So you only have to change the place where that property is displayed. In DetailViewController.m, change this method:

- (NSDateFormatter *)dateFormatter {    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];  //REMOVE
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];  //REMOVE
    }
    return dateFormatter;
}

To this:

- (NSDateFormatter *)dateFormatter {    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEEE MMMM d"];  //NEW LINE
    }
    return dateFormatter;
}

Edit: Forgot to mention that the actual display is done in cellForRowAtIndexPath in the same file. But that display code uses a class-level variable dateFormatter so we just modify its initialization.

Edit #2: You modified the sample code to group the rows by the book copyright date. To modify the date format in the section heading there is probably a more elegant way to do it but here is a crude solution. Modify titleForHeaderInSection in RootViewController.m to this:

- (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;
}

The change above assumes you will only do sections by date. If not, you'll need to check whether the current grouping is by date otherwise it will try to convert author's name or title to a date and crash. You may also want to add a check for null/invalid date.

Edit #3: The copyright field is NSDate which contains both date and time. To group by date only, when the copyright field is edited, set the time part to 00 as follows (in save method in EditingViewController.m):

    // Pass current value to the edited object, then pop.
if (editingDate) {
    NSDate *pickedDateTime = datePicker.date;

    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-dd 00:00:00 -0000"];
    NSString *pickedDateStr = [formatter stringFromDate:pickedDateTime];
    pickedDateTime = [formatter dateFromString:pickedDateStr];

    [editedObject setValue:pickedDateTime forKey:editedFieldKey];
}

HOWEVER, since the sample data provided in the example from Apple already includes time in the copyright field, editing or adding one book will not put it in the same group as another group with the same date because the other book still has the time portion in it. If you edit both books and save, it will clear the time in both and they will show in the same group.

A better solution would be to somehow tell the SortDescriptor to only look at the date portion of copyright only instead of the copyright as-is but I'm not sure how to do this yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消