0

我已成功在我的应用程序中集成 Kal 日历,这是我显示日历的方法

-(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 以便它显示标有事件的日期并选择它在月视图下方可用的表视图中显示事件详细信息。
如果您是 Kal 日历的新手,请参阅此链接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 次)。

即使我想限制这个日历不显示过去的日期!请帮我解决这个问题。

4

1 回答 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,tableView:cellForRowAtIndexPath:请在您的KalDataSource类中实现,就像在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选择了 a,请定义一个实现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];
    }
    
于 2013-05-06T15:06:16.613 回答