0

我正在将MBCalendarKit与 Core Data 连接起来。

这是构建事件字典的代码:

for (Task *task in _taskArray)
{
    _event = [CKCalendarEvent eventWithTitle:[task valueForKey:@"title"] andDate:[task valueForKey:@"startDate"] andInfo:nil];
    [_eventsArray addObject:_event];
}
 // Moving events to Dictionary
[_calendarData setObject:_eventsArray forKey:_event.date];

在实现方法CKCalendarDataSource时,结果不是我们想要的。我为日历中的每个日期获取数组中的所有事件,如下所示。

- (NSArray *)calendarView:(CKCalendarView *)calendarView eventsForDate:(NSDate *)date {
   return _calendarData[_event.date];
}

在此处输入图像描述

4

1 回答 1

0

由于CKCalendarDataSource需要一个特定的字典格式,我不得不遍历 eventsArray 而不是仅仅将它传递给 _calendarData。

{
    "July 15, 2014" =     (
        "<CKCalendarEvent: 0x1123128e0>"
    );
    "July 16, 2014" =     (
        "<CKCalendarEvent: 0x11230fb10>",
        "<CKCalendarEvent: 0x11230ff00>"

    "July 18, 2014" =     (
        "<CKCalendarEvent: 0x11230d880>",
        "<CKCalendarEvent: 0x11230df30>"
    );
}

[eventsArray enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {

    if ([_calendarData objectForKey:[[eventsArray objectAtIndex:idx]date]]) {
        // Dictionary already contains object with this date

        NSMutableArray *existingDate = [[NSMutableArray alloc]init];
        [existingDate addObjectsFromArray:[_calendarData objectForKey:[[eventsArray objectAtIndex:idx]date]]];
        [existingDate addObject:object];

        [_calendarData setObject:existingDate forKey:[[eventsArray objectAtIndex:idx]date]];
    }

    else {
        NSMutableArray *uniqueDate = [[NSMutableArray alloc]init];
        [uniqueDate addObject:object];
        [_calendarData setObject:uniqueDate forKey:[[eventsArray objectAtIndex:idx]date]];
    }
}];
于 2014-07-25T02:48:58.540 回答