【问题标题】:How to select multiple dates in TapKu library?TapKu库中如何选择多个日期?
【发布时间】:2014-09-20 21:15:38
【问题描述】:

我想显示 tapkilibrary 中的多个选定日期 .like highlight 14aug2011 到 18aug2011 之间的日期> .

【问题讨论】:

    标签: xcode tapku


    【解决方案1】:
        -(NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate{
    
    NSLog(@"Date Selected is %@",date);
            //txtbdate.text=date;
    
        NSDateFormatter *timeFormat = [[[NSDateFormatter alloc] init] autorelease];
        NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        [timeFormat setDateFormat:@"yyyy-MM-dd"];
        [timeFormat setTimeZone:gmt];
    
    
        //[timeFormat setLocale:[NSLocale currentLocale]];
        //[timeFormat setTimeZone:[NSTimeZone localTimeZone]]; 
        NSString *theTime = [timeFormat stringFromDate:date];   
        NSLog(@"%@",theTime); 
        objappdel.strdate=theTime;
    
        [tkmonthView reload];
        AppointmentDetail *appointmentDetail=[[AppointmentDetail alloc]initWithNibName:@"AppointmentDetail" bundle:nil];
        [self.navigationController pushViewController:appointmentDetail animated:YES];
        [appointmentDetail release];
    }
    
    - (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate
    {   
        NSMutableArray * data = [[NSMutableArray alloc] init];
        NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
        NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        [dateForm setDateFormat:@"yyyy-MM-dd"];
        [dateForm setTimeZone:gmt];
        NSDate *date ;
    
    
        for (int i=0; i<[objappdel.arrDate count]; i++)
        {
            NSString *time;
            time=[objappdel.arrDate objectAtIndex:i];
            //time=  [[[jobData valueForKey:@"Record"] objectAtIndex:i] valueForKey:@"JobStartDate"];
            //time = [[time componentsSeparatedByString:@" "] objectAtIndex:0];
            date = [dateForm dateFromString:time];
            [data addObject:[NSString stringWithFormat:@"%@",date]];
        }
        NSArray *copy = [data copy];
            NSInteger index = [copy count] - 1;
            for (id object in [copy reverseObjectEnumerator]) 
            {
                if ([data indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) 
                {
                    [data removeObjectAtIndex:index];
                }
                index--;
            }
    
        NSLog(@"sorted dates are %@",copy);
        // 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:(NSYearCalendarUnit | NSMonthCalendarUnit |
                                                  NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | 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
            //NSLog(@"%@",[d description]);
    
            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];
        }
    
        [offsetComponents release];
    
        return [NSArray arrayWithArray:marks];
    }
    

    使用此委托方法。它将返回您要突出显示的日期的 NSArray。

    【讨论】:

    • 有趣的是,您相当简单和简短的答案都获得了大约 10 票。
    • @Dhara 您的代码中的 ApppointmenDetail 和 tkMonthView 是什么?此外,它不会从代码中返回任何数组。我确定它不起作用!其余代码来自 - developinginthedark.com/posts/… 演示代码。不是吗?我正在尝试做更多的事情。如果可能的话,你能回答这个吗 - stackoverflow.com/questions/9686684/… 谢谢。
    • @AppleDeveloper tkMonthView 是 TkMonthView 类的对象。 ApppointmenDetail 是我的本地课程。好吧,我从 gitHub 获得了代码。
    【解决方案2】:

    您可以通过首先将日期输入到数组中来做到这一点。代码是。

     - (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
    NSLog(@"selected Date IS - %@",inDate);
    
    [myArray addObject:d];
    
    for (id entry in myArray)
    {
    
        if (inDate == nil && outDate == nil)
        {
            inDate = d;
            outDate = d;
        }
        if ([d compare:inDate] == NSOrderedAscending)
        {
            inDate = d;
        }
        if ([d compare:outDate] == NSOrderedDescending)
        {
            outDate = d;
        }
    
        d = nil;
    }
    

    }

    在此之后,您必须使用按钮单击操作,通过该操作您可以在这两个日期之间选择日期。它的代码是:

     - (IBAction)goBtn:(id)sender
      {
    NSLog(@"startDate is: %@",inDate);
    NSLog(@"endDate is: %@",outDate);
    
    [calendar reload];
    inDate = nil;
    outDate = nil;
    
    }
    

    }

    然后在一个委托方法中,您只需创建一个包含这两个日期之间的所有日期的数组。它将在按钮单击后被调用。它的代码是:

     - (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {
    //***********
    NSMutableArray *tempData = [[NSMutableArray alloc] init];
    NSDate *nextDate;
    for ( nextDate = inDate ; [nextDate compare:outDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
        // use date
        NSLog(@"%@",nextDate);
        [tempData addObject:[NSString stringWithFormat:@"%@",nextDate]];
    }
    [tempData addObject:[NSString stringWithFormat:@"%@",outDate]];
    //***********
    
    
    NSMutableArray *marks = [NSMutableArray array];
    
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |
                                              NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit)
                                    fromDate:startDate];
    NSDate *d = [cal dateFromComponents:comp];
    
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];
    
    
    while (YES) {
        if ([d compare:lastDate] == NSOrderedDescending) {
            break;
        }
    
        if ([tempData containsObject:[d description]]) {
            [marks addObject:[NSNumber numberWithBool:YES]];
        } else {
            [marks addObject:[NSNumber numberWithBool:NO]];
        }
    
        d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
    }
    
    return [NSArray arrayWithArray:marks];
    

    }

    希望对你有所帮助。如果您遇到任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多