2013-02-05 82 views
0

我在我的應用程序集成卡爾日曆成功,這裏是我的方法如何表現日曆KAL日曆,如何實現KalDataSource,以便我可以顯示iOS內置日曆應用程序中的事件?

-(void)showDepartDatePicker{ 
    NSLog(@"showDepartDatePicker"); 
    if(_departDatePicker != nil){ 
     [self.navigationController pushViewController:_departDatePicker animated:YES]; 
    }else{ 
     _departDatePicker = [[KalViewController alloc] init]; 
     _departDatePicker.title = @"Departure Date"; 
     _departDatePicker.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:self action:@selector(showAndSelectTodayDeparturePicker)]; 
     _departDatePicker.kvcDelegate = self; 
     [self.navigationController pushViewController:_departDatePicker animated:YES]; 
    } 

} 

我已經加入KalViewController.h以下,

@protocol KalViewControllerDelegate <NSObject> 
@required 
- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded; 

@end 

@property (nonatomic, assign) id <KalViewControllerDelegate> kvcDelegate; 

並在我的viewController中實現了這個委託方法,如

- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded 
{ 
    NSLog(@"Title : %@",[self.navigationController.visibleViewController title]); 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

現在,根據我的問題,我想要實現KalDataSource,以便顯示標記事件的日期並選擇它在月視圖下方的表視圖中顯示事件詳細信息。
如果你是新的卡爾日曆https://github.com/klazuka/Kal

第二個問題,請參考此鏈接,這裏是我如何調用從KalViewController.m委託方法

- (void)didSelectDate:(KalDate *)date 
{ 
    self.selectedDate = [date NSDate]; 
    NSDate *from = [[date NSDate] cc_dateByMovingToBeginningOfDay]; 
    NSDate *to = [[date NSDate] cc_dateByMovingToEndOfDay]; 
    [self clearTable]; 
    [dataSource loadItemsFromDate:from toDate:to]; 
    [tableView reloadData]; 
    [tableView flashScrollIndicators]; 
    //line below calls my delegate method 
    [self.kvcDelegate didSelectDate:date andLoaded:_loaded]; 
} 

會發生什麼事,當我打電話showDepartDatePicker推KalViewController到我的導航堆棧,它調用我的委託方法2次(這應該在日期選擇上調用),然後爲每個日期選擇再次調用委託方法(1次)。

即使我想限制此日曆不顯示過去的日期! 請幫我解決這個問題。

回答

1
  1. 定義一個實現KalDataSource協議的類。請參閱下面的示例,瞭解實施KalDataSource協議的類。

    //header file 
    #import Kal.h" 
    @interface MyClass : NSObject <KalDataSource> 
    
    @property (nonatomic, weak) id<KalDataSourceCallbacks> kalCallbackDelegate; 
    @property (nonatomic, strong) NSArray *events; 
    
    @end 
    
    ---------------------- 
    //implementation file 
    
    - (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)callbackDelegate 
    { 
        //If you already have the events between fromDate and toDate then just call 
        [callbackDelegate loadedDataSource:self]; 
    
        //Else store the callback variable in a property and do an asyncrhonous 
        //call to load the events. 
        self.kalCallbackDelegate = callbackDelegate; 
    
        //When the Asynchronous call is done, call 
        [self.kalCallbackDelgate loadedDataSource:self]; 
    
    } 
    
    - (void)removeAllItems 
    { 
        self.eventsForDay = nil; 
    } 
    
    - (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate 
    { 
        //self.events may have multiple events with the same date. This pulls only the unique dates. 
        //Also assumes that the object has an eventDate property for the beginning of the day 
        NSMutableSet *uniqueDatesSet = [NSMutableSet setWithArray:[self.events valueForKeyPath:@"@distinctUnionOfObjects.eventDate"]]; 
    
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self >= %@ && self <= %@", fromDate, toDate]; 
        NSArray *uniqueDates = [[uniqueDatesSet allObjects] filteredArrayUsingPredicate:predicate]; 
    
        return uniqueDates; 
    
    } 
    
    - (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate 
    { 
        //filter for the events that occur between fromDate and toDate 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"eventDate >= %@ && eventDate <= %@", fromDate, toDate]; 
        NSArray *filteredArray = [self.events filteredArrayUsingPredicate:predicate]; 
    
        self.eventsForDay = [filteredArray sortedArrayUsingSelector:@selector(compareByEventTime:)]; 
    } 
    
  2. 要渲染UITableViewCells,在你KalDataSource類實現tableView:cellForRowAtIndexPath:就像你會爲UITableView

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        Event *event = [self.events objectAtIndex:indexPath.row]; 
    
        static NSString *identifier = @"MyCell"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
        if (!cell) { 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
         cell.selectionStyle = UITableViewCellSelectionStyleNone; 
        } 
    
        cell.textLabel.text = event.title; 
    
        return cell; 
    } 
    
  3. 如果你想在選擇一個UITableViewCell知道,定義一個類實現UITableViewDelegate協議並設置_departDatePicker.delegate等於類。然後,您可以在該類中實施常規方法UITableViewDelegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        Event *event = [self.events objectAtIndex:indexPath.row]; 
        MyViewController *viewController = [[UIStoryboard storyboardWithName:@"iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"eventInfo"]; 
        viewController.event = event; 
        [self.navigationController pushViewController:viewController animated:YES]; 
    } 
    
相關問題