0

シナリオ:

私は経費追跡iOSアプリケーションを持っており、経費詳細ビューコントローラーからの経費をカテゴリと金額および日付とともに経費のリストを表示するテーブルビュー(フェッチされた結果コントローラーを含む)に保存しています。私のエンティティ「Money」には、費用または収入のいずれかの親エンティティである日付属性があります。

質問:

基本的に、特定の週、月、または年の経費を分類し、セクションヘッダーのタイトルとして表示します(例:(2012年10月1日から10月7日))。それに応じて経費額と関連情報が表示されます。特定の週。そのビューには2つのボタンがあり、右ボタンを押すと週が1週間ずつ増え(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します。@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

以前のプロジェクトの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変数に設定します。prevClickとnextClickという名前のUIActionを持つ2つのUIButtonがあり、次または前の週の開始を設定するメソッドを呼び出します。

- (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 に答える