1

plistに保存された日付でカレンダーをマークしたい

ここに画像の説明を入力

そして私はこのコードをmarksFromDateの中に入れました

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate
{        
    NSString *documentDirectory = [self applicationDocumentsDirectory];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"PlainNotes.plist"];
    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
    self.Notes = tmpArray;
    NSString *date = [tmpArray valueForKey:@"CDate"];

    NSArray *data = [NSArray arrayWithObject:date];
    NSLog(@"Mark for this date %@", data);

    // Initialise empty marks array, this will be populated with TRUE/FALSE in order for each day a marker should be placed on.
    NSMutableArray *marks = [NSMutableArray array];

    // Initialise calendar to current type and set the timezone to never have daylight saving
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // Construct DateComponents based on startDate so the iterating date can be created.
    // Its massively important to do this assigning via the NSCalendar and NSDateComponents because of daylight saving has been removed 
    // with the timezone that was set above. If you just used "startDate" directly (ie, NSDate *date = startDate;) as the first 
    // iterating date then times would go up and down based on daylight savings.
    NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | 
                                              NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) 
                                    fromDate:startDate];
    NSDate *d = [cal dateFromComponents:comp];

    // Init offset components to increment days in the loop by one each time
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];    


    // for each date between start date and end date check if they exist in the data array
    while (YES) {
        // Is the date beyond the last date? If so, exit the loop.
        // NSOrderedDescending = the left value is greater than the right
        if ([d compare:lastDate] == NSOrderedDescending) {
            break;
        }

        // If the date is in the data array, add it to the marks array, else don't
        if ([data containsObject:[d description]]) {
            [marks addObject:[NSNumber numberWithBool:YES]];
        } else {
            [marks addObject:[NSNumber numberWithBool:NO]];
        }

        // Increment day using offset components (ie, 1 day in this instance)
        d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
    }


    return [NSArray arrayWithArray:marks];
}

そして、これはログ用です

2012-08-19 17:10:50.168 PlainNotes[2852:f803] Mark for this date (
        (
        "2012-08-19 00:00:00 +0000",
        "2012-08-31 00:00:00 +0000"
    )
)

これは私のカレンダーをマークしませんでしたが、以下のコードは TapkuCalendar からのものです

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {    
    NSLog(@"calendarMonthView marksFromDate toDate");   
    NSLog(@"Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.");
    // When testing initially you will have to update the dates in this array so they are visible at the
    // time frame you are testing the code.
    NSArray *data = [NSArray arrayWithObjects:
                     @"2011-01-01 00:00:00 +0000", @"2011-01-09 00:00:00 +0000", @"2011-01-22 00:00:00 +0000",
                     @"2011-01-10 00:00:00 +0000", @"2011-01-11 00:00:00 +0000", @"2011-01-12 00:00:00 +0000",
                     @"2011-01-15 00:00:00 +0000", @"2011-01-28 00:00:00 +0000", @"2011-01-04 00:00:00 +0000",                   
                     @"2011-01-16 00:00:00 +0000", @"2011-01-18 00:00:00 +0000", @"2011-01-19 00:00:00 +0000",                   
                     @"2011-01-23 00:00:00 +0000", @"2011-01-24 00:00:00 +0000", @"2011-01-25 00:00:00 +0000",                                       
                     @"2011-02-01 00:00:00 +0000", @"2011-03-01 00:00:00 +0000", @"2011-04-01 00:00:00 +0000",
                     @"2011-05-01 00:00:00 +0000", @"2011-06-01 00:00:00 +0000", @"2011-07-01 00:00:00 +0000",
                     @"2011-08-01 00:00:00 +0000", @"2011-09-01 00:00:00 +0000", @"2011-10-01 00:00:00 +0000",
                     @"2011-11-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil]; 
    NSLog(@"Mark for this date %@", data);

これはTapkuCalendarログ用です

2012-08-19 17:01:08.604 TapkuCalendarTabBarDemo[2807:f803] calendarMonthView marksFromDate toDate
2012-08-19 17:01:08.605 TapkuCalendarTabBarDemo[2807:f803] Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.
2012-08-19 17:01:08.606 TapkuCalendarTabBarDemo[2807:f803] Mark for this date (
    "2011-01-01 00:00:00 +0000",
    "2011-01-09 00:00:00 +0000",
    "2011-01-22 00:00:00 +0000",
    "2011-01-10 00:00:00 +0000",
    "2011-01-11 00:00:00 +0000",
    "2011-01-12 00:00:00 +0000",
    "2011-01-15 00:00:00 +0000",
    "2011-01-28 00:00:00 +0000",
    "2011-01-04 00:00:00 +0000",
    "2011-01-16 00:00:00 +0000",
    "2011-01-18 00:00:00 +0000",
    "2011-01-19 00:00:00 +0000",
    "2011-01-23 00:00:00 +0000",
    "2011-01-24 00:00:00 +0000",
    "2011-01-25 00:00:00 +0000",
    "2011-02-01 00:00:00 +0000",
    "2011-03-01 00:00:00 +0000",
    "2011-04-01 00:00:00 +0000",
    "2011-05-01 00:00:00 +0000",
    "2011-06-01 00:00:00 +0000",
    "2011-07-01 00:00:00 +0000",
    "2011-08-01 00:00:00 +0000",
    "2011-09-01 00:00:00 +0000",
    "2011-10-01 00:00:00 +0000",
    "2011-11-01 00:00:00 +0000",
    "2011-12-01 00:00:00 +0000"
)

ここに表示されているのは、私のログと TapkuCalendar のログの両方が同じですが、なぜ私のコードでカレンダーをマークできないのですか? 修正方法は?ありがとうございました。

4

2 に答える 2

1

メソッドは次のように定義されている-(NSArray*)...ため、最初の懸念は、何も返さないことです。

return data;の末尾に追加してみてくださいcalendarMonthView:marksFromDate:toDate:。`

于 2012-08-19T09:27:38.763 に答える
0

私はすでにそれを解決します。以下のコードのように、NSString を NSArray に変更します。

NSArray *data = [Notes valueForKeyPath:@"CDate"];
NSLog(@"Mark for this date %@", data);

助けてくれてありがとう。本当に感謝しております。私の愚かな質問で申し訳ありません。私は初心者です。

于 2012-08-19T11:38:53.597 に答える