0

任何人都可以帮助我解决我遇到的问题吗?

我正在使用 Tapku 日历库日视图来显示事件。首次加载日历时,事件会正确显示。如果我将日期更改为昨天或明天,则日历不会显示事件。

我已经实现了以下事件。

- (void)calendarDayTimelineView:(TKCalendarDayView *)calendarDay didMoveToDate:(NSDate *)date
{
//Here is my logic to pull the data from db server.
//After this I am calling the method below.
[self.dayView reloadData];
}

    - (NSArray *) calendarDayTimelineView:(TKCalendarDayView*)calendarDayTimeline eventsForDate:(NSDate *)eventDate{
    self.myAppointments = nil;
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Tasks" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
    self.myAppointments = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return @[];
    if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return @[];

    NSDateComponents *info = [[NSDate date] dateComponentsWithTimeZone:calendarDayTimeline.timeZone];
    info.second = 0;
    NSMutableArray *ret = [NSMutableArray array];

    for(Tasks *apt in self.myAppointments){

        TKCalendarDayEventView *event = [calendarDayTimeline dequeueReusableEventView];
        if(event == nil) event = [TKCalendarDayEventView eventView];

        event.identifier = nil;
        event.titleLabel.text = apt.task_subject;

        if ( [allTrim(apt.location) length] != 0 )
        {
            event.locationLabel.text = apt.location;
        }

        NSDate *startDate = apt.task_start;
        NSDate *endDate = apt.task_end;

        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:startDate];
        NSInteger hour = [components hour];
        NSInteger minute = [components minute];

        info.hour = hour;
        info.minute = minute;
        event.startDate = [NSDate dateWithDateComponents:info];

        components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:endDate];
        hour = [components hour];
        minute = [components minute];


        info.hour = hour;
        info.minute = minute;
        event.endDate = [NSDate dateWithDateComponents:info];

        [ret addObject:event];

    }

    return ret;
}

我已经调试了代码,数据被分配给事件,但是我无法查看日历上的任何内容。

请..帮助解决这个问题。

问候,

gvnsandeep

4

1 回答 1

1

还有另一种方法可以解决这个问题。

如您所见,在以下方法中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"Day View", @"");


    self.data = @[
  @[@"Meeting with five random dudes", @"Five Guys", @960, @0, @1000, @30],
  @[@"Unlimited bread rolls got me sprung", @"Olive Garden", @7, @0, @12, @0],
  @[@"Appointment", @"Dennys", @15, @0, @18, @0],
  @[@"Hamburger Bliss", @"Wendys", @15, @0, @18, @0],
  @[@"Fishy Fishy Fishfelayyyyyyyy", @"McDonalds", @5, @30, @6, @0],
  @[@"Turkey Time...... oh wait", @"Chick-fela", @14, @0, @19, @0],
  @[@"Greet the king at the castle", @"Burger King", @19, @30, @100, @0]];  
}

self.data 已加载。

如果您进行一些编写,我们可以将加载过程移动到“viewWillAppear”方法来更新数据。请注意,没有将事件添加到特定日期的方法。但是我们可以像这样添加一个事件:@[@"Fishy Fishy Fishfelayyyyyyyy", @"McDonalds", @48, @00, @50, @0]。@48 表示后天的事件。

因此,首先您应该计算特定日期和 self.dayview.date(today) 之间的时间间隔。以下代码可能会有所帮助:

- (NSArray *)getTimeFromNow:(NSDate *)startDate endDateTime:(NSDate *)endDate
{
  NSDate *todayDate = [NSDate date];
  NSTimeInterval startFromNowSeconds = [startDate timeIntervalSinceDate:todayDate];
  NSTimeInterval endFromNowSeconds = [endDate timeIntervalSinceDate:todayDate];

  NSNumber *startHour = [NSNumber numberWithInt:startFromNowSeconds / (60 * 60)];
  NSNumber *startMinute = [NSNumber numberWithInt:startFromNowSeconds / 60 - startHour.intValue * 60];

  NSNumber *endHour = [NSNumber numberWithInt:endFromNowSeconds / (60 * 60)];
  NSNumber *endMinute = [NSNumber numberWithInt:endFromNowSeconds / 60 - endHour.intValue * 60];

  return @[startHour, startMinute, endHour, endMinute];
}

下面应该评论:

if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]] == NSOrderedAscending) return @[];
if([eventDate compare:[NSDate dateWithTimeIntervalSinceNow:24*60*60]] == NSOrderedDescending) return @[];

但要注意时区。我认为返回上面计算的正确时间数组并不容易。

此外,我认为有必要在 DayViewController 中添加将事件添加到特定日期的方法。

注意:由于我已经进行了一些编码,如果您的时间是从现在开始的@900 小时,则该事件将不会显示在 dayview 中......但@500 小时它运作良好。

这里的设计多么糟糕!等待更完整的 dayviewcontroller ...

于 2013-09-24T05:38:44.107 回答