日期部分标题不起作用

时间:2011-11-13 16:55:23

标签: ios uitableview core-data nsfetchedresultscontroller

我在tableview上遇到了一个问题。 我拿了Apple的例子: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)一个部分为部分名称键路径'sectionIdentifier'返回了nil值。对象将放在未命名的部分

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

,就会发生这种情况
NSDateComponents *components = [calendar components:
   (NSMonthCalendarUnit | NSDayCalendarUnit) 
   fromDate:[self timeStamp]];

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

相关问题