如何将 Tapku 日历框架与 NSFetchRequest 结合使用?

发布于 2024-11-16 01:13:51 字数 3164 浏览 1 评论 0原文

我正在尝试让 Tapku 日历库在我的应用程序中使用核心数据。我已经设置了日历,但我现在想做的是让点标记显示保存在我的核心数据中的日期。

我正在尝试遵循这个示例 (http://developinginthedark.com/posts/iphone-tapku -calendar-markers),但在将其与核心数据混合时我陷入困境。

我需要使用的方法是:

- (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-12-01 00:00:00 +0000", nil]; 


    // 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];
    }

    [offsetComponents release];

    return [NSArray arrayWithArray:marks];
}

I'm trying to get the Tapku Calendar Library working with core data in my app. I have got the calendar setup but what I am trying to do now is get the dot markers to display for dates which are saved in my core data.

I am trying to follow this example (http://developinginthedark.com/posts/iphone-tapku-calendar-markers) but I'm stuck when mixing it with core data.

The method I need to work with is:

- (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-12-01 00:00:00 +0000", nil]; 


    // 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];
    }

    [offsetComponents release];

    return [NSArray arrayWithArray:marks];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2024-11-23 01:13:51

有一个像 TKCalendarMonthView 这样的类检查它将根据您从核心数据给出的数组来显示点。核心数据中的数组(日期)将与日历中显示的特定月份的日期进行比较,如果两个日期匹配,则 BOOL 将返回,因为它将标记为“.”。

there is a class like TKCalendarMonthView check that which will have dots to be displayed depending upon the array which you will give from core data. the array from your core data which will be a date will compared with dates for the particular month which you show in calendar if both dates matches then BOOL will return as it will marked with "."..

楠木可依 2024-11-23 01:13:51

您可以使用以下方法来做到这一点:

     - (void) generateDataForStartDate:(NSDate*)start endDate:(NSDate*)end
        {

                 //just check the condition according to you...It;s your logic that would work
                 if([datestring isEqualToString:strtemp])
                    {
                        Done=YES;
                        [dataDictionary setObject:str forKey:d];//USed to show the data
                        [dataArray addObject:[NSNumber numberWithBool:YES]]; // This array would store the place where you need to set the Dot in Calender
                        j=1;
                    }
        // This is the logic for not setting the DOT
         if(j==0){
            [dataArray addObject:[NSNumber numberWithBool:NO]];
                }

        TKDateInformation info = [d dateInformation];
        info.day++;
        Done=NO;
        d = [NSDate dateFromDateInformation:info];
        if([d compare:end]==NSOrderedDescending) break;


        }

希望这能解决您的问题....否则您可以自己设置 [dataArray addObject:[NSNumber numberWithBool:NO]];根据您的情况选择是/否。

You could do that using this method:

     - (void) generateDataForStartDate:(NSDate*)start endDate:(NSDate*)end
        {

                 //just check the condition according to you...It;s your logic that would work
                 if([datestring isEqualToString:strtemp])
                    {
                        Done=YES;
                        [dataDictionary setObject:str forKey:d];//USed to show the data
                        [dataArray addObject:[NSNumber numberWithBool:YES]]; // This array would store the place where you need to set the Dot in Calender
                        j=1;
                    }
        // This is the logic for not setting the DOT
         if(j==0){
            [dataArray addObject:[NSNumber numberWithBool:NO]];
                }

        TKDateInformation info = [d dateInformation];
        info.day++;
        Done=NO;
        d = [NSDate dateFromDateInformation:info];
        if([d compare:end]==NSOrderedDescending) break;


        }

Hope that would solve your problem....else you could so it by your own just setting up the [dataArray addObject:[NSNumber numberWithBool:NO]]; to yes/no based on your condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文