0

我正在使用Kal Calendar向我的应用程序添加日历视图,但在事件列表上实现 didSelectRowAtIndexPath 方法时遇到问题。当用户选择任何一天的事件时,我想推送一个视图控制器。我尝试将方法放在“KalView.m”、“KalViewController.m”和“KalDataSource.m”中,但没有一个被识别。调用这种方法的合适位置在哪里?

4

1 回答 1

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
于 2013-03-01T04:03:09.137 回答