uitableview部分标题内的日期格式,请帮助 [英] Date formatting inside the uitableview section heading, PLEASE HELP

查看:162
本文介绍了uitableview部分标题内的日期格式,请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始说我非常新的iphone开发,但我试图真的很难学习,所以任何帮助你的专业人士愿意分享是非常感谢!所以我有一个问题,如果有人可以回答我真的太棒了。我正在研究更多的一个核心数据,并且一直在使用苹果开发者网站的核心数据书示例,发现 here 。这是一个非常直接的应用程序,但我正在努力改变一些东西,我无法弄清楚如何做到这一点,这正在推动我CRAZY!本来,应用程序在表视图部分标题中显示作者,并在单元格中显示标题。我想更改它,并将版权日期(该书的一个属性)设置为部分标题。现在,我可以看到它显示,但它显示以下格式的日期:



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



但是这不是正确的格式,Id喜欢使用这种格式:



星期二12月1日



可能使用这段代码:



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



[outputFormatter setDateFormat:@EEEE MMMM d];



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



但问题是日期值来自datepicker,我无法弄清楚(所有这些疯狂的关键业务)如何格式化来自选择器的日期值并将其放在节标题上。如果你有任何时间可能跟随上面的苹果wbsite的链接,捅到你可以回答我的dilema,它应该被认可!!!谢谢。



好的,这里是我把代码放在保存方法中:

  //将当前值传递给编辑对象,然后弹出。 
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];
}

然后无论什么原因,日期不会保存在应用程序中,编译器抛出这个错误:



Debugger已退出状态0.
[会话始于2009-12-02 09:51:26 -0700。 ]
2009-12-02 09:51:47.342 CoreDataBooks [17981:20b]
* - [__ NSCFDate length]:无法识别的选择器发送到实例0x3e79850
2009-12-02 09:51:47.343 CoreDataBooks [17981:20b] *
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'*** - [__ NSCFDate length]:无法识别的选择器发送到实例0x3e79850'



可能只是一个快速的修复,我希望至少在代码中的右半球,再次感谢您有时间提供的任何帮助或洞察力。

解决方案

您不必担心关键业务至少在此更改日期格式。书籍对象中的版权属性将使用EditingViewController中的关键业务自动更改。



所以您只需要更改显示该属性的位置。
在DetailViewController.m中,更改此方法:

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

为此:

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

编辑:忘了说实际的显示是在相同的cellForRowAtIndexPath中完成的文件。但是显示代码使用类级变量dateFormatter,所以我们只是修改它的初始化。



编辑#2:你修改了示例代码,按照图书版权日期。要修改部分标题中的日期格式,可能是一个更优雅的方式来做,但这里是一个粗略的解决方案。将RootViewController.m中的titleForHeaderInSection修改为:

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
NSString * rawDateStr = [[[fetchedResultsController sections] objectAtIndex:section] name];

//将默认日期字符串转换为NSDate ...
NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss ZZ];
NSDate * date = [formatter dateFromString:rawDateStr];

//将NSDate转换为我们想要的格式...
[formatter setDateFormat:@EEEE MMMM d];
NSString * formattedDateStr = [formatter stringFromDate:date];

return formattedDateStr;
}

上面的更改假设您只会按日期进行部分。如果没有,您将需要检查当前分组是否按日期,否则将尝试将作者的名称或标题转换为日期并崩溃。您可能还想添加一个无效/无效日期的支票。



编辑#3:版权字段是包含日期和时间的NSDate。要按日期分组,当编辑版权字段时,将时间部分设置为00,如下所示(在EditingViewController.m中的保存方法中):

  //将当前值传递给编辑对象,然后弹出。 
if(editingDate){
NSDate * tookDateTime = datePicker.date;

NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@yyyy-MM-dd 00:00:00 -0000];
NSString * tookDateStr = [formatter stringFromDate:sampledDateTime];
copiedDateTime = [formatter dateFromString:sampledDateStr];

[editedObject setValue:takenDateTime forKey:editedFieldKey];
}

然而,由于苹果示例中提供的示例数据已经包含了版权字段,编辑或添加一本书不会将其与具有相同日期的另一个组放在同一组中,因为另一本书仍然具有时间部分。如果您编辑这两本书并保存,它将清除两者中的时间,并显示在同一个组中。



更好的解决方案是以某种方式告诉SortDescriptor只看版权的日期部分,而不是版权原样,但我不知道该怎么做。


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 picker 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.

这篇关于uitableview部分标题内的日期格式,请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆