2014-07-21 73 views
0

我正在連接MBCalendarKit和核心數據。MBCalendarKit:將CKCalendarDataSource連接到核心數據

這是建立事件的字典的代碼:

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

enter image description here

回答

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