date部分标题不起作用
我有一个按部分按datetableview的问题。 我拿了苹果的例子: 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)部分名称关键path'sectionIdentifier'的部分返回nil值。 对象将被放置在未命名的部分
你知道为什么吗 ? 谢谢你的帮助 !
从逻辑上讲,如果在你的代码的这一行,会发生这种情况
NSDateComponents *components = [calendar components: (NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[self timeStamp]];
[self timeStamp]
返回一个无效的NSDate
。 如果是这种情况,请检查NSLog
语句。
一个常见的错误通常是为nsmanaged对象编码瞬态属性,人们忘记在数据模型文件(xcdatamodeld)中“启用”瞬态属性。