日期部分标题不起作用

编程入门 行业动态 更新时间:2024-10-11 19:16:03
本文介绍了日期部分标题不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在按日期列出的表视图上遇到问题。 我以Apple为例: DateSectionTitles

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 :

在我的CoreData类中:

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;}

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

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:错误:(NSFetchedResultsController)一个节返回了节名称键路径 sectionIdentifier的nil值。对象将放置在未命名的部分

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

Do you know why ? Thanks for your help !

推荐答案

从逻辑上讲,如果在代码的这一行会发生这种情况

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

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

[self timeStamp] 返回无效的 NSDate 。如果是这种情况,请使用 NSLog 语句进行检查。

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

更多推荐

日期部分标题不起作用

本文发布于:2023-11-26 20:30:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   日期   标题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!