日期部分标题不起作用
我在按日期划分的表格视图上遇到问题。 我以苹果为例: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常为 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).
从逻辑上讲,如果在代码的这一行
[self timeStamp]
返回无效的NSDate
,就会发生这种情况。如果是这种情况,请检查 NSLog 语句。Logically, that would happen if at this line of your code
[self timeStamp]
returns an invalidNSDate
. Check withNSLog
statements if that is the case.