// // timeManageSheetView.m // ADHTuanCan // // Created by 敖德亨 on 2023/10/7. // #import "timeManageSheetView.h" #import "MonthModel.h" #import "MonthTableViewCell.h" #import "ADHTuanCan-Swift.h" @interface timeManageSheetView () /// tableviw父视图 @property (weak, nonatomic) IBOutlet UIView *backView; /// 计数 @property (weak, nonatomic) IBOutlet UILabel *countNumLab; @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSMutableArray *dataArray; @property(nonatomic,strong)UIView *weekView; /// 日历开始时间 @property(nonatomic,strong)NSDate * viewBeginDate; /// 选择开始时间 @property(nonatomic,strong)NSDate * startDate; /// 选择结束时间 @property(nonatomic,strong)NSDate * endDate; @property(nonatomic,strong)DayModel * startDayModel; @property(nonatomic,strong)DayModel * endDayModel; @end @implementation timeManageSheetView - (void)awakeFromNib{ [super awakeFromNib]; // self.backgroundColor = [UIColor whiteColor]; [self.backView addSubview:self.weekView]; [self.backView addSubview:self.tableView]; self.dataArray = [NSMutableArray array]; } - (void)dealloc { NSLog(@"%s",__FUNCTION__); } /////////////////////////////////////创建视图/////////////////////////////////////////// #pragma mark - 创建主视图 -(UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width, 390) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.showsVerticalScrollIndicator = NO; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellID"]; } return _tableView; } #pragma mark - 创建星期视图 -(UIView *)weekView{ if (!_weekView) { _weekView = [[UIView alloc]initWithFrame:CGRectMake(-1, 0, self.bounds.size.width+2, 40)]; NSMutableArray * title = [NSMutableArray array]; NSArray *chtitle = @[@"一",@"二",@"三",@"四",@"五",@"六",@"日"]; NSArray *entitle = @[@"Mon",@"Tues",@"Wed",@"Thur",@"Fri",@"Sat",@"Sun"]; if (LanguagesUtil.isEnLanguage) { [title setArray:entitle]; }else{ [title setArray:chtitle]; } for (int i =0 ; i < 7 ; i++) { UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width/7*i+1, 0, self.bounds.size.width/7, _weekView.bounds.size.height)]; label.textAlignment = NSTextAlignmentCenter; label.text = title[i]; if (i>4) { label.textColor = [UIColor orangeColor]; } [_weekView addSubview:label]; } _weekView.backgroundColor = [UIColor whiteColor]; _weekView.layer.borderWidth = 1; _weekView.layer.masksToBounds = YES; _weekView.layer.borderColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1].CGColor; } return _weekView; } /////////////////////////////////////点击事件/////////////////////////////////////////// #pragma mark - 确定事件返回 - (IBAction)saveAction:(UIButton *)sender { if (self.startDate == nil) { return; } if (self.endDate == nil) { return; } [self selectedCheckDate]; } -(void)selectedCheckDate{ NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate]; NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString *startDateStr = [dateFormatter stringFromDate:self.startDate]; NSString *endDateStr = [dateFormatter stringFromDate:self.endDate]; NSString *daysStr = [NSString stringWithFormat:@"%ld",days + 1]; [self hidden]; if (_selectCheckDateBlock) { _selectCheckDateBlock(startDateStr,endDateStr,daysStr); } } /////////////////////////////////////代理方法/////////////////////////////////////////// -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } -(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ MonthModel *model = self.dataArray[indexPath.row]; return model.cellHight; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MonthTableViewCell *cell = [MonthTableViewCell cellWithTableView:tableView]; cell.model = self.dataArray[indexPath.row]; __weak typeof(self) weakSelf = self; cell.selectedDay = ^(DayModel *returnDaymodel){ NSDate * nowdate = NSDate.now; self.countNumLab.text = @""; if ([self compareDate:returnDaymodel.dayDate withDate:self.startDate] == 1) { for (MonthModel *Mo in self.dataArray) { for (DayModel *mo in Mo.days) { mo.state = DayModelStateNormal; mo.Daysort = 0; } } self.startDate = nil; self.endDate = nil; self.startDate = returnDaymodel.dayDate; returnDaymodel.state = DayModelStateStart; returnDaymodel.Daysort = 1; returnDaymodel.isEndTimeOK = NO; self.startDayModel = returnDaymodel; [self.tableView reloadData]; return; } if ((self.startDate != nil) && (self.endDate != nil)){ for (MonthModel *Mo in self.dataArray) { for (DayModel *mo in Mo.days) { mo.state = DayModelStateNormal; mo.Daysort = 0; } } self.startDate = nil; self.endDate = nil; self.startDate = returnDaymodel.dayDate; returnDaymodel.state = DayModelStateStart; returnDaymodel.Daysort = 1; returnDaymodel.isEndTimeOK = NO; self.startDayModel = returnDaymodel; [self.tableView reloadData]; return; } if (self.endDate == nil) { self.endDate = returnDaymodel.dayDate; for (MonthModel *Mo in self.dataArray) { for (DayModel *mo in Mo.days) { mo.state = DayModelStateNormal; mo.Daysort = 0; if ([self compareDate:mo.dayDate withDate:self.endDate] == 1 && [self compareDate:mo.dayDate withDate:self.startDate] == -1){ mo.state = DayModelStateSelected; NSInteger days = [self calcDaysFromBegin:self.startDate end:mo.dayDate]; mo.Daysort = days + 1; } if (mo.dayDate == self.startDate) { self.startDate = mo.dayDate; self.startDayModel = mo; mo.state = DayModelStateStart; mo.Daysort = 1; mo.isEndTimeOK = YES; } } } NSInteger days1 = [self calcDaysFromBegin:self.startDate end:returnDaymodel.dayDate]; returnDaymodel.Daysort = days1 + 1; returnDaymodel.state = DayModelStateEnd; self.endDayModel = returnDaymodel; NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate]; self.countNumLab.text = [LanguagesUtil createTextByCtext:[NSString stringWithFormat:@"共计%d天",days + 1] Etext:[NSString stringWithFormat:@"%d days in total",days + 1]]; [self.tableView reloadData]; return; } }; return cell; } /////////////////////////////////////数据处理/////////////////////////////////////////// #pragma mark - 懒加载数据源 -(void)setDateArr{ [self.dataArray removeAllObjects]; NSInteger days = [self calcDaysFromBegin:self.startDate end:self.endDate]; self.countNumLab.text = [LanguagesUtil createTextByCtext:[NSString stringWithFormat:@"共计%d天",days] Etext:[NSString stringWithFormat:@"%d days in total",days]]; NSDate *nowdate = NSDate.now; NSInteger toYear = [self getDataFromDate:nowdate type:@"year"]; NSInteger toMonth = [self getDataFromDate:nowdate type:@"month"]; for (int i = 0; i<2; i++) { if (i == 0) { MonthModel * monthModel = [[MonthModel alloc] init]; monthModel.year = toYear; monthModel.month = toMonth; NSMutableArray *days = [NSMutableArray array ]; NSInteger starNum = [self getDataFromDate :self.viewBeginDate type:@"day"]; for (NSInteger i = starNum ; i <=[self totaldaysInMonth:nowdate]; i++) { DayModel *dayModel = [[DayModel alloc]init]; dayModel.dayDate = [self dateWithYear:monthModel.year month:monthModel.month day:i]; dayModel.day = i; dayModel.month = monthModel.month; dayModel.year = monthModel.year; dayModel.dayOfTheWeek = [self getDataFromDate:dayModel.dayDate type:@"week"]; dayModel.isToday = i==starNum; dayModel.state = DayModelStateNormal; dayModel.Daysort = 0; if ([self compareDate:dayModel.dayDate withDate:nowdate] == 1) { dayModel.isStatePast = YES; } if ([self compareDate:dayModel.dayDate withDate:self.startDate] == 0) { dayModel.state = DayModelStateStart; dayModel.Daysort = 1; dayModel.isEndTimeOK = YES; self.startDayModel = dayModel; } if ([self compareDate:dayModel.dayDate withDate:self.endDate] == 0) { dayModel.state = DayModelStateEnd; dayModel.Daysort = [NSString stringWithFormat:@"%ld",[self calcDaysFromBegin:self.startDate end:dayModel.dayDate]].intValue + 1; //差值需要加1 self.endDayModel = dayModel; } if ([self compareDate:dayModel.dayDate withDate:self.endDate] == 1 && [self compareDate:dayModel.dayDate withDate:self.startDate] == -1){ dayModel.state = DayModelStateSelected; dayModel.Daysort = [NSString stringWithFormat:@"%ld",[self calcDaysFromBegin:self.startDate end:dayModel.dayDate]].intValue + 1; //差值需要加1 } [days addObject:dayModel]; } monthModel.days = days; DayModel *m = days.firstObject; NSInteger lineCount = 1; NSInteger oneLineCoune =( 7 - m.dayOfTheWeek + 2 ) % 7; if (oneLineCoune == 0) { oneLineCoune = 7; } NSInteger count = days.count - oneLineCoune; if (count%7==0) { lineCount = lineCount + count/7 ; }else{ lineCount = lineCount + count/7 + 1 ; } monthModel.cellNum = lineCount * 7; monthModel.cellStartNum = 7 - oneLineCoune ; monthModel.cellHight = 60 + 60 * lineCount + 2 * (lineCount + 1); [_dataArray addObject:monthModel]; toMonth++; }else{ if (toMonth == 13) { toMonth = 1; toYear += 1; } NSDate *toDate = [self dateWithYear:toYear month:toMonth day:1]; MonthModel * monthModel = [[MonthModel alloc] init]; monthModel.year = [self getDataFromDate:toDate type:@"year"]; monthModel.month = [self getDataFromDate:toDate type:@"month"]; NSMutableArray *days = [NSMutableArray array ]; for (NSInteger i = 1 ; i <=[self totaldaysInMonth:toDate]; i++) { DayModel *dayModel = [[DayModel alloc]init]; dayModel.dayDate = [self dateWithYear:monthModel.year month:monthModel.month day:i]; dayModel.day = i; dayModel.month = monthModel.month; dayModel.year = monthModel.year; dayModel.dayOfTheWeek = [self getDataFromDate:dayModel.dayDate type:@"week"]; dayModel.isToday = NO; dayModel.state = DayModelStateNormal; [days addObject:dayModel]; } monthModel.days = days; DayModel *m = days.firstObject; NSInteger lineCount = 1; NSInteger oneLineCoune =( 7 - m.dayOfTheWeek + 2 ) % 7; if (oneLineCoune == 0) { oneLineCoune = 7; } NSInteger count = days.count - oneLineCoune; if (count%7==0) { lineCount = lineCount + count/7 ; }else{ lineCount = lineCount + count/7 + 1 ; } monthModel.cellNum = lineCount * 7; monthModel.cellStartNum = 7 - oneLineCoune ; monthModel.cellHight = 60 + 60 * lineCount + 2 * (lineCount + 1); [_dataArray addObject:monthModel]; toMonth++; } } [self.tableView reloadData]; } #pragma mark - 获取年,月,日,星期 注:日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。 -(NSInteger )getDataFromDate:(NSDate *)date type:(NSString * )type{ NSCalendar *calendar = nil; if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) { calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]; }else{ calendar = [NSCalendar currentCalendar]; } NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday) fromDate:date]; if ([type isEqualToString:@"year"]) { return components.year; }else if ([type isEqualToString:@"month"]) { return components.month; }else if ([type isEqualToString:@"day"]) { return components.day; }else if ([type isEqualToString:@"week"]) { return components.weekday; }else{ return 0; } } #pragma mark -- 获取当前月共有多少天 - (NSInteger)totaldaysInMonth:(NSDate *)date{ NSRange daysInLastMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date]; return daysInLastMonth.length; } #pragma mark - 时间字符串转时间 -(NSDate *)dateWithYear:(NSInteger )year month:(NSInteger )month day:(NSInteger )day { NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyyMMdd"]; return [formatter dateFromString:[NSString stringWithFormat:@"%ld%02ld%02ld",year,month,day]]; } #pragma mark-日期比较 -(NSInteger )compareDate:(NSDate *)date01 withDate:(NSDate *)date02{ NSInteger ci; NSComparisonResult result = [date01 compare:date02]; switch (result) { //date02比date01大 case NSOrderedAscending: ci=1; break; //date02比date01小 case NSOrderedDescending: ci=-1; break; //date02=date01 case NSOrderedSame: ci=0; break; default: NSLog(@"erorr dates %@, %@", date02, date01); break; } return ci; } #pragma mark - <#content#> 计算两个日期之间的天数 - (NSInteger) calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate{ //创建日期格式化对象 NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; //取两个日期对象的时间间隔: //这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval; NSTimeInterval time=[endDate timeIntervalSinceDate:beginDate]; int days=((int)time)/(3600*24); //int hours=((int)time)%(3600*24)/3600; //NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时",days,hours]; return days; } - (IBAction)closeAction:(UIButton *)sender { [self hidden]; } -(void)showWithStaStr:(NSString *) staStr endStr:(NSString *)endStr beginDate:(NSDate *)beginDate{ NSString * begYear = [staStr substringWithRange:NSMakeRange(0,4)]; NSString * begMonth = [staStr substringWithRange:NSMakeRange(5,2)]; NSString * begDay = [staStr substringWithRange:NSMakeRange(8,2)]; NSString * endYear = [endStr substringWithRange:NSMakeRange(0,4)]; NSString * endMonth = [endStr substringWithRange:NSMakeRange(5,2)]; NSString * endDay = [endStr substringWithRange:NSMakeRange(8,2)]; self.startDate = [self dateWithYear:begYear.intValue month:begMonth.intValue day:begDay.intValue]; self.endDate = [self dateWithYear:endYear.intValue month:endMonth.intValue day:endDay.intValue]; self.viewBeginDate = beginDate; [self setDateArr]; self.frame = [UIApplication sharedApplication].keyWindow.bounds; [[UIApplication sharedApplication].keyWindow addSubview:self]; } -(void)hidden{ [self removeFromSuperview]; } @end