2013-12-14 72 views
2

我想做一個預約應用程序,用戶可以選擇一個日期或日期範圍預約約會。爲此我使用DSLCalenadarView這是工作正常根據我的要求,但問題它我想要顯示當前日期自動選擇,其次我無法找到數據處理如何工作,因爲我想存儲選擇日期,但沒有找到任何完美的解決方案。DSLCalendarView數據處理和當前日期

有人可以幫我整合這個。

回答

7

找到並打開DSLCalendarDayView.m現在找thie方法,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; 
     } 
    } 
} 
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];