Vurig-Calendar icon indicating copy to clipboard operation
Vurig-Calendar copied to clipboard

I want to mark several dates in a month with an Array

Open ygoez opened this issue 11 years ago • 1 comments

If i have several dates to mark in a month, it marks only the last date!!?? Please Help

here is the code:

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated { NSArray *test = [NSArray arrayWithObjects:@"2013-07-22",@"2013-07-23",@"2013-08-09",nil];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];


NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

for (int i=0; i<test.count;i++){
     NSDate *mydate = [dateFormat dateFromString:[test objectAtIndex:i]];
     NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:mydate];

if (month == components.month) {
    NSArray *date = [NSArray arrayWithObjects:mydate, nil];
    NSArray *color = [NSArray arrayWithObjects:[UIColor redColor],nil];
    [calendarView markDates:date withColors:color];
    }
}

}

ygoez avatar Jul 11 '13 12:07 ygoez

I know this question was asked long time ago. But I don't think this is a problem about the library. Calendar marked the last date because -markDates was called only once. Please refer to the code below.

 __block NSMutableArray *dateMarkers = [[NSMutableArray alloc] init];
 __block NSMutableArray *dateMarkersColor = [[NSMutableArray alloc] init];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSArray *test = [NSArray arrayWithObjects:@"2013-07-22",@"2013-07-23",@"2013-08-09",nil];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    for (int i=0; i<test.count;i++){
        NSDate *mydate = [dateFormat dateFromString:[test objectAtIndex:i]];
        NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:mydate];

        if (month == components.month) {
            [dateMarkers addObject:mydate];
            [dateMarkersColor addObject:[UIColor redColor]];
        }
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [calendarView markDates:dateMarkers withColors:dateMarkersColor];
    });
});

johnpaulmanoza avatar May 23 '14 06:05 johnpaulmanoza