日期部分标题不起作用

发布于 2024-12-15 06:38:13 字数 1244 浏览 2 评论 0原文

我在按日期划分的表格视图上遇到问题。 我以苹果为例: DateSectionTitles

我不知道不在乎年份。我需要月日。 所以我这样调整我的代码:

在我的 CoreData 类中:

- (NSString *)sectionIdentifier {
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
NSLog(@"!Temp");
if (!tmp) {
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [calendar components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[self timeStamp]];
    tmp = [NSString stringWithFormat:@"%d", ([components month]*100) + [components day]];
    [self setPrimitiveSectionIdentifier:tmp];
}
return tmp;}

在我的主控制器中的 titleForHeaderInSection 方法中:

NSInteger month = numericSection / 100;
NSInteger day = numericSection - (month * 100);

NSString *titleString = [NSString stringWithFormat:@"%d %d",day, month];

return titleString;

但是当我运行我的应用程序时,我收到此消息:

CoreData:错误:(NSFetchedResultsController) A 部分返回 nil 值节名称键路径“sectionIdentifier”。对象将被放置在未命名的部分

你知道为什么吗? 感谢您的帮助 !

I have a problem on a tableview with section by date.
I took the Apple example : DateSectionTitles

I don't care about year. I ust need month and day.
So I adapt my code like that :

In my CoreData class :

- (NSString *)sectionIdentifier {
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
NSLog(@"!Temp");
if (!tmp) {
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [calendar components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[self timeStamp]];
    tmp = [NSString stringWithFormat:@"%d", ([components month]*100) + [components day]];
    [self setPrimitiveSectionIdentifier:tmp];
}
return tmp;}

And in my titleForHeaderInSection method in my main controller :

NSInteger month = numericSection / 100;
NSInteger day = numericSection - (month * 100);

NSString *titleString = [NSString stringWithFormat:@"%d %d",day, month];

return titleString;

But when I run my app I have this message :

CoreData: error: (NSFetchedResultsController) A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section

Do you know why ?
Thanks for your help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-12-22 06:38:13

通常为 nsmanagedobject 编码瞬态属性的一个常见错误是人们忘记在数据模型文件 (xcdatamodeld) 中也“启用”瞬态属性。

One common mistake usually coding transient property for nsmanagedobject, is people forget to also "enable" transient property in the data model file (xcdatamodeld).

单挑你×的.吻 2024-12-22 06:38:13

从逻辑上讲,如果在代码的这一行

NSDateComponents *components = [calendar components:
   (NSMonthCalendarUnit | NSDayCalendarUnit) 
   fromDate:[self timeStamp]];

[self timeStamp]返回无效的NSDate,就会发生这种情况。如果是这种情况,请检查 NSLog 语句。

Logically, that would happen if at this line of your code

NSDateComponents *components = [calendar components:
   (NSMonthCalendarUnit | NSDayCalendarUnit) 
   fromDate:[self timeStamp]];

[self timeStamp] returns an invalid NSDate. Check with NSLog statements if that is the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文