2011-11-13 27 views
1

我有一个tableview按部分日期的问题。 我拿了苹果的例子: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:error:(NSFetchedResultsController)节的名称键路径返回nil值' sectionIdentifier”。对象将被放置在未命名的部分

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

回答

0

从逻辑上讲,这将在这行代码

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

[self timeStamp]返回无效NSDate的发生,如果。如果是这种情况,请联系NSLog声明。

+0

没有打印。它不进入 - (NSString *)sectionIdentifier方法:/ – Pierre

+0

好吧,太好了。你发现了这个问题。 – Mundi

+0

是的,但我不知道为什么:D – Pierre

3

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

+0

启用瞬态属性保存了我的一天。谢谢! –