[英]KAL Calendar, how to implement KalDataSource so as I can show events which are in iOS in-built Calendar app?
I have integrated Kal Calendar in my app successfully, here is the method how I show the calendar 我已成功将Kal Calendar集成到我的应用中,这是显示日历的方法
-(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];
}
}
I have added following in KalViewController.h, 我在KalViewController.h中添加了以下内容,
@protocol KalViewControllerDelegate <NSObject>
@required
- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded;
@end
and 和
@property (nonatomic, assign) id <KalViewControllerDelegate> kvcDelegate;
and implemented this delegate method in my viewController as 并在我的viewController中实现了这个委托方法
- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded
{
NSLog(@"Title : %@",[self.navigationController.visibleViewController title]);
[self.navigationController popViewControllerAnimated:YES];
}
now, as per my question, I want to implement KalDataSource so as it show the day marked with events and selecting it show the event details in the table view available below Month's View. 现在,按照我的问题,我想实现KalDataSource,以便它显示带有事件标记的日期,并选择它在Month's View下方的表格视图中显示事件详细信息。
Refer this link if you are new for Kal Calendar https://github.com/klazuka/Kal 如果您是Kal Calendar的新用户,请参考此链接https://github.com/klazuka/Kal
Second Question, here is how I call delegate method from KalViewController.m 第二个问题,这是我如何从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];
}
What happens is, when I call showDepartDatePicker to push KalViewController to my navigation stack, it calls my delegate method 2 times(which should be called on date selection), then for every date selection calls that delegate method again(1 time). 发生的是,当我调用showDepartDatePicker将KalViewController推送到导航堆栈时,它将调用我的委托方法2次(应在日期选择时调用),然后对于每个日期选择再次调用该委托方法(1次)。
Even I want to limit this calendar not to show past dates! 即使我也想限制此日历不显示过去的日期! Please help me out on this. 请帮助我。
Define a class which implements the KalDataSource
protocol. 定义一个实现KalDataSource
协议的类。 See below example for class implementing KalDataSource
protocol. 有关实现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:)]; }
To render the UITableViewCells, implement tableView:cellForRowAtIndexPath:
in your KalDataSource
class just like you would for a UITableView
. 要呈现UITableViewCells,请像在UITableView
那样在KalDataSource
类中实现tableView:cellForRowAtIndexPath:
KalDataSource
- (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; }
If you want to know when a UITableViewCell
is selected, define a class implementing the UITableViewDelegate
protocol and set _departDatePicker.delegate
equal to that class. 如果您想知道何时选择UITableViewCell
,请定义实现UITableViewDelegate
协议的类,并将_departDatePicker.delegate
设置_departDatePicker.delegate
等于该类。 Then you can implement the regular UITableViewDelegate
methods in that class. 然后,您可以在该类中实现常规的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]; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.