我想预约应用程序,用户可以选择单个日期或日期范围来预约。为此,我使用DSLCalenadarView这可以正常工作,但我想要显示当前日期的问题自动选择,其次我无法找到数据处理的工作原理,因为我想存储选择日期,但没有找到任何完美的解决方案。
请帮我整合一下。
答案 0 :(得分:8)
查找并打开DSLCalendarDayView.m
现在查找此方法,drawBackground
- (void)drawBackground {
if (self.selectionState == DSLCalendarDayViewNotSelected) {
NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:flags fromDate:[self.day date]];
NSDateComponents *componentsOfToday = [calendar components:flags fromDate:[NSDate date]];
NSDate *date = [calendar dateFromComponents:components];
NSDate *dateToday = [calendar dateFromComponents:componentsOfToday];
if (self.isInCurrentMonth) {
if ([date isEqualToDate:dateToday]) {
[[UIColor lightGrayColor] setFill]; //from now current day is colored
}
else {
[[UIColor colorWithWhite:245.0 / 255.0 alpha:1.0] setFill];
}
}
else {
if ([date isEqualToDate:dateToday]) {
[[UIColor lightGrayColor] setFill]; //even if it is not current month, current day is colored
}
else {
[[UIColor colorWithWhite:225.0 / 255.0 alpha:1.0] setFill];
}
}
UIRectFill(self.bounds);
}
else {
switch (self.selectionState) {
case DSLCalendarDayViewNotSelected:
break;
case DSLCalendarDayViewStartOfSelection:
[[[UIImage imageNamed:@"DSLCalendarDaySelection-left"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
break;
case DSLCalendarDayViewEndOfSelection:
[[[UIImage imageNamed:@"DSLCalendarDaySelection-right"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
break;
case DSLCalendarDayViewWithinSelection:
[[[UIImage imageNamed:@"DSLCalendarDaySelection-middle"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
break;
case DSLCalendarDayViewWholeSelection:
[[[UIImage imageNamed:@"DSLCalendarDaySelection"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)] drawInRect:self.bounds];
break;
}
}
}
答案 1 :(得分:2)
您可以使用setSelectedRange来执行此操作。以下是自动选择当前日期的代码段。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarCalendarUnit |
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSWeekdayCalendarUnit |
NSSecondCalendarUnit) fromDate:[NSDate date]];
DSLCalendarRange *range = [[DSLCalendarRange alloc]initWithStartDay:components endDay:components];
[self.calendarView setSelectedRange:range];