使用Kal日历选择事件

时间:2013-02-14 18:10:57

标签: iphone uitableview kal

我正在使用Kal Calendar向我的应用添加日历视图,但在事件列表中实现didSelectRowAtIndexPath方法时遇到问题。当用户选择任何给定日期的事件时,我想推送一个视图控制器。我已经尝试将该方法放在“KalView.m”,“KalViewController.m”和“KalDataSource.m”中,但都没有被识别。适当的地方称这种方法在哪里?

1 个答案:

答案 0 :(得分:1)

我曾经遇到过类似的问题,以下是我实施的方法。

#import <MTDates/NSDate+MTDates.h>
#import <ObjectiveSugar/ObjectiveSugar.h>
#import <UIImageView+WebCache.h>

#import "EventsViewController.h"
#import "EventDetailsViewController.h"
#import "EventCell.h"
#import "Event.h"

#import "KalViewController.h"
#import "CalendarViewController.h"

@implementation EventsViewController

- (id)initWithEvents:(NSArray *)_events {
    self = [super init];
    events = _events;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self listUpdated];
    [self addCalendarView];
}

- (void)listUpdated
    {

        NSMutableArray *allEvents = [NSMutableArray array];
        [allEvents addObjectsFromArray:events];

        NSArray *sortedArray = [allEvents sortedArrayUsingComparator:^NSComparisonResult(Event *obj1, Event *obj2) {
            return [obj1.date compare:obj2.date];
        }];

        _allEvents = sortedArray;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_allEvents count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//custom table cell (EventCell is a view I'm initialising my rows with)
    Event *event = _allEvents[indexPath.row];
    NSString *reuseIdentifier = [NSString stringWithFormat:@"Cell%@%@", event.venue.identifier, event.identifier];
    EventCell *cell = (EventCell*) [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [EventCell createDetailedCellWithReuseIdentifier:reuseIdentifier];
        cell.nameLabel.text = event.name;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Event *event = _allEvents[indexPath.row];
    EventDetailsViewController *controller = [[EventDetailsViewController alloc] initWithEvent:event];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void) addCalendarView{
    _calenderView = [[KalViewController alloc] initWithSelectedDate:[NSDate date]];
    [[self.view viewWithTag:2] addSubview:_calenderView.view]; /* depends on your requirements*/
    _calenderView.view.tag = 200; /* not necessary */
    [_calenderView.view setFrame:self.view.bounds];
    _calenderView.dataSource = self;
    _calenderView.delegate = self;
}

- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
    //NSLog(@"%@, %@", fromDate, toDate);
    // filter and pass the array to the events tableview
    [self filterByDateSelected:toDate];
}

-(void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
   /** when selecting a different month **/
}

- (void)removeAllItems
{
    NSLog(@"Items Removed");
    // remove all the previous items from the tableview
}

-(void) filterByDateSelected: (NSDate *)selectedDate
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];

    NSString *theDate = [dateFormat stringFromDate:selectedDate];
    NSDate *_date = [NSDate dateFromString:theDate usingFormat:@"yyyy-MM-dd"];

    // filter table by selectedDate
    NSArray *_dateFilteredEvents = _allEvents;

    _dateFilteredEvents = [_allEvents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Event *evaluatedEvent, NSDictionary *bindings) {
        if ([evaluatedEvent.date isEqualToDate:_date]) {
            return YES;
        }
        return NO;
    }]];
    NSLog(@"%@", _dateFilteredEvents);

     //  uncomment the following line if you want to display list in another controller 
    //[self showEventsByDate:_dateFilteredEvents];

   // or else refresh table after updating the list
  _allEvents = _dateFilteredEvents;
        [_tableView reloadData];
}

- (void) showEventsByDate:(NSArray*)events
{
    if (events.count > 0) {
        CalendarViewController *_controller = [[CalendarViewController alloc] initWithEventArray:events];
        [self.navigationController pushViewController:_controller animated:YES];
    }
}

- (void) removeCalenderView{
    [_calenderView.view removeFromSuperview];
}

@end