tapkulibrary icon indicating copy to clipboard operation
tapkulibrary copied to clipboard

TKCalendarMonthView does not display current month after reload

Open Nikooos opened this issue 12 years ago • 0 comments

I'm using TKCalendarMonthTableViewController to display events from the EKEventStore which I have to ask permisson to first. So when I have permission I'm reloading the monthView, but then the current month is not displaying. If you use the arrows, you can go to the next month which display correctly and then you can slide back to have a perfectly displaying month. But why isn't it showing up in the first place? Any thoughts?

Another crazy thing is that sometimes, after a while, it does display the code. But this takes very long and not set to a particular timeframe. First the image not working and second working, but only after scrolling through months. img_0040 img_0041

Code is below

#import "MonthCalenderViewController.h"

@interface MonthCalenderViewController ()
@property (nonatomic,strong) EKEventStore *store;
@property (nonatomic,strong) NSArray *calendars;
@end

@implementation MonthCalenderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.monthView selectDate:[NSDate date]];

    self.store = [[EKEventStore alloc] init];
    [self.store requestAccessToEntityType:EKEntityTypeEvent
                          completion:^(BOOL granted, NSError *error) {
                              if (granted) {
                                  self.calendars = [self.store calendarsForEntityType:EKEntityTypeEvent];

                                  [self.monthView reloadData];
                                  [self.monthView selectDate:[NSDate date]];
                                  [self.tableView reloadData];
                              }
                          }];
}

#pragma mark MonthView Delegate & DataSource
- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
    //[self generateRandomDataForStartDate:startDate endDate:lastDate];

    NSArray *data;
    self.dataArray = [NSMutableArray array];
    self.dataDictionary = [NSMutableDictionary dictionary];

    NSDate *d = startDate;
    while(YES){

        NSPredicate *predicate = [self.store predicateForEventsWithStartDate:d endDate:[d dateByAddingTimeInterval:60*60*24] calendars:self.calendars];

        // Fetch all events that match the predicate.
        data = [self.store eventsMatchingPredicate:predicate];

        if ([data count] > 0) {
            //(self.dataDictionary)[d] = @[@"Item one",@"Item two"];
            NSMutableArray *eventsArray = [[NSMutableArray alloc] init];
            for (EKEvent *event in data) {
                [eventsArray addObject:event.title];
            }
            (self.dataDictionary)[d] = eventsArray;
            [self.dataArray addObject:@YES];
        } else {
            [self.dataArray addObject:@NO];
        }


        NSDateComponents *info = [d dateComponentsWithTimeZone:self.monthView.timeZone];
        info.day++;
        d = [NSDate dateWithDateComponents:info];
        if([d compare:lastDate]==NSOrderedDescending) {
            break;
        }
    }

    return self.dataArray;
}
- (void) calendarMonthView:(TKCalendarMonthView*)monthView didSelectDate:(NSDate*)date{
    NSLog(@"Date Selected: %@",date);
    [self.tableView reloadData];
}
- (void) calendarMonthView:(TKCalendarMonthView*)mv monthDidChange:(NSDate*)d animated:(BOOL)animated{
    [super calendarMonthView:mv monthDidChange:d animated:animated];
    [self.tableView reloadData];
}


#pragma mark UITableView Delegate & DataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *ar = self.dataDictionary[[self.monthView dateSelected]];
    if(ar == nil) return 0;
    return [ar count];
}
- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    NSArray *ar = self.dataDictionary[[self.monthView dateSelected]];
    cell.textLabel.text = ar[indexPath.row];

    return cell;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Nikooos avatar Oct 14 '13 10:10 Nikooos