0

设想:

我有一个费用跟踪 iOS 应用程序,我将费用从费用详细视图控制器存储到表视图(带有获取的结果控制器)中,该表视图显示费用列表以及类别、金额和日期。我的实体“Money”中确实有一个日期属性,它是支出或收入的父实体。

问题:

我想要的是基本上对给定一周、一个月或一年的费用进行分类,并将其显示为部分标题标题,例如:(2012 年 10 月 1 日至 10 月 7 日),并据此显示费用金额和相关内容特定的一周。该视图中提供了两个按钮,如果我按下右侧按钮,它将一周增加一周(2012 年 10 月 1 日至 10 月 7 日现在显示 2012 年 10 月 8 日至 10 月 15 日),同样,左侧按钮将减少一周一个星期。

我将如何做到这一点?我正在尝试以下代码 - 不起作用。

- (void)weekCalculation
{
   NSDate *today = [NSDate date];  // present date (current date)

   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:today];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

递增日期的代码 -

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

}

假设星期显示如下(2012 年 11 月 4 日 - 2012 年 11 月 10 日),我按下增量按钮,我在控制台中看到,日期更改为 2012 年 11 月 11 日和 2012 年 11 月 17 日,这是正确的,但如果我按下增量按钮再次,它再次显示相同的日期(2012 年 11 月 11 日和 2012 年 11 月 17 日)。

请帮帮我。

4

2 回答 2

2

在您的班级中声明currentDate为 a 。@property试试这个。

@property(nonatomic, retain) NSDate *currentDate;

初始设置

self.currentDate = [NSDate date];

在调用此方法之前。

- (void)weekCalculation
{
   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:self.currentDate];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

一旦视图被加载,self.currentDate值应该从showNextDates. 确保它没有在其他任何地方重置。

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

   self.currentDate = newDate1;

}
于 2012-11-09T08:15:10.353 回答
1

我在一个旧项目中需要类似的东西,并通过下面的代码实现了它。它将本周日期设置为 NSDate 变量,并在每个按钮单击中添加/删除 7 天的日期组件。这是代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *date = [NSDate date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:date ];
//
[components setDay:([components day] - ([components weekday] )+2)];
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

上面的代码计算本周开始并将其设置为 currentDate 变量。我有两个 UIButtons 与 UIActions 命名 prevClick 和 nextClick 调用设置下一个或上一个 weekstart 的方法:

- (IBAction)prevClick:(id)sender {
[self addRemoveWeek:NO];

}

-(void)addRemoveWeek:(BOOL)add{
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:self.currentDate ];
components.day = add?components.day+7:components.day-7;
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

}

- (IBAction)nextClk:(id)sender {
[self addRemoveWeek: YES];

}

于 2012-11-09T08:12:47.080 回答