CalendarView 优化方法

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


@property (nonatomic, strong) MTDailyView  * DailyView;

@property (nonatomic, copy) NSString * startDate;

@property (nonatomic, copy) NSString * endDate;

@property (nonatomic, copy) NSString * ParamStartDate;

@property (nonatomic, copy) NSString * ParamEndDate;

@property (nonatomic , assign)NSInteger daysNum;  //日

@property (nonatomic , assign)NSInteger monthsNum; //月份

@property (nonatomic , assign)NSInteger yearsNum;  //年份

self.DailyView = [[MTDailyView alloc] initWithFrame:CGRectMake(0, 0, MTScreenWidth, viewY(110))];

self.DailyView.delegate = self;

[self.view addSubview:self.DailyView];

//起始日期-结束日期

NSString * date = @"";

int y = [[date getThisyear] intValue];

int m = [[date getThisMonth] intValue];

int dayinM = [self getDaysInMonth:y month:m];

_ParamStartDate = [NSString stringWithFormat:@"%d-%d-%d",y,m,01];

_ParamEndDate = [NSString stringWithFormat:@"%d-%d-%d",y,m,dayinM];


- (int)getDaysInMonth:(int)year month:(int)imonth {

// imonth == 0的情况是应对在CourseViewController里month-1的情况

if((imonth == 0)||(imonth == 1)||(imonth == 3)||(imonth == 5)||(imonth == 7)||(imonth == 8)||(imonth == 10)||(imonth == 12))

return 31;

if((imonth == 4)||(imonth == 6)||(imonth == 9)||(imonth == 11))

return 30;

if((year%4 == 1)||(year%4 == 2)||(year%4 == 3))

{

return 28;

}

if(year%400 == 0)

return 29;

if(year%100 == 0)

return 28;

return 29;

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/**

重新设置日历日期-->传进来一个日期,显示全月的日历,日期格式为:2016-01-01;上月、下月的点击事件可以通过此方法来实现

因为不管请求数据是否成功都需要显示某月日期,所以重新设置日期和设置背景色的方法分开*/

-(void)reloadCalendarViewWithDate:(NSString *)date;

/**

重载方法1:如果只有一种背景色要设置可以用这个方法

arr:arr中的每个元素是一个字典:@[@{@"date":@"2016-01-01"}]

key:通过key取到arr中字典中设置颜色的日期,当前情况下key传@“date”

color:设置的颜色    */

-(void)reloadCalendarViewWithArr:(NSArray *)arr color:(UIColor *)dateColor key:(NSString *)key;

/**重载方法2:有数据的日期为请假、出勤、缺勤等多种数据时

DictArr为二维数组,第一维分成请假、出勤、缺勤,

第二维中的第0个元素是保存数据的字典(需要通过传过来的key取字典中的数据)

第1个元素是当前类型的数据显示的背景色 */

-(void)reloadCalendarViewWithDataDictArr:(NSArray *)DictArr key:(NSString *)key;

/*

DictArr格式为 :

@[

@[  @[  @{@"date":@"2016-01-01"},@{@"date":@"2016-01-01"}  ],

绿色

];

@[  @[  @{@"date":@"2016-01-01"},@{@"date":@"2016-01-01"}  ],

红色

]

];

*/

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//获取当前日期,以及年、月、日数字

[self setupCurrentDate];

//获取某月天数、获取第一天周几、获取日历有几行

[self setUpCalendar];



#pragma mark - 本月月份+1 -

-(NSArray * )currentMonthAddWithYear:(NSInteger )year month:(NSInteger )month{

if (month + 1 > 12) {

year  ++ ;

month = 1;

}

else{

month ++ ;

}

return @[[NSNumber numberWithInteger:year],[NSNumber numberWithInteger:month]];

}

#pragma mark - 本月月份-1 -

-(NSArray * )currentMonthCutWithYear:(NSInteger )year month:(NSInteger )month{

if (month - 1 < 1) {

year  -- ;

month = 12;

}

else{

month -- ;

}

return @[[NSNumber numberWithInteger:year],[NSNumber numberWithInteger:month]];

}

#pragma mark - 传进来一个时间重载CollectionView -

-(void)reloadCalendarViewWithDate:(NSString *)date{

_startDate = @"0";

_endDate = @"0";

_indexPathNum = 42;

_calendarModelNum = 1;

NSMutableArray * dateArr = [NSMutableArray arrayWithArray:[self cutString:date bySymbol:@"-"]];

self.yearsNum = [dateArr[0] integerValue];

self.monthsNum = [dateArr[1] integerValue];

self.daysNum = [dateArr[2] integerValue];

self.nxetMonthDays = 1 ;

//在日历行显示的上个月灰色的天数

NSArray * lastMonthArr = [self currentMonthCutWithYear:self.yearsNum month:self.monthsNum];

self.lastMonthDays = [self numberOfDaysInYears:[lastMonthArr[0] integerValue] month:[lastMonthArr[1] integerValue]];

[self setUpCalendar];//获取某月天数、获取第一天周几、获取日历有几行

[self.calendarCollectionView reloadData];

}

#pragma mark - 重载方法1: -

-(void)reloadCalendarViewWithArr:(NSArray *)arr color:(UIColor *)dateColor key:(NSString *)key{

//只有一种数据要显示背景色的时候用这个重载方法,比如只显示有服药的日期时可以用,下面cellForItemAtIndexPath里面有数据的日期需要修改

_indexPathNum = 0;

self.dataArr = [NSArray arrayWithArray:arr];

self.key = key ;

self.dateColor = dateColor ;

self.nxetMonthDays = 1 ;

//在日历行显示的上个月灰色的天数

NSArray * lastMonthArr = [self currentMonthCutWithYear:self.yearsNum month:self.monthsNum];

self.lastMonthDays = [self numberOfDaysInYears:[lastMonthArr[0] integerValue] month:[lastMonthArr[1] integerValue]];

[self.calendarCollectionView reloadData];

}

#pragma mark - 重载方法2: -

-(void)reloadCalendarViewWithDataDictArr:(NSArray *)DictArr key:(NSString *)key{

//    有多种数据要显示背景色的时候用这个重载方法,

//    比如有请假、出勤、缺勤等显示不同背景色可以用,只需要在初始化的时候把数据按照一定格式放到初始化方法2的DictArr中

//    DictArr数据格式:一维是请假、出勤、缺勤等的数据,二维中的第0个元素是保存数据的数组(这样的话貌似是三维数组),第1个元素是当前类型的数据显示的背景色

self.nxetMonthDays = 1 ;

self.key = key ;

//在日历行显示的上个月灰色的天数

NSArray * lastMonthArr = [self currentMonthCutWithYear:self.yearsNum month:self.monthsNum];

self.lastMonthDays = [self numberOfDaysInYears:[lastMonthArr[0] integerValue] month:[lastMonthArr[1] integerValue]];

self.dataDictArr = [NSArray arrayWithArray:DictArr];

[self.calendarCollectionView reloadData];

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/*

//点击的cell下标不能小于本月第一天,也不能超过本月最后一天

if (indexPath.row >= self.firsrtDay && indexPath.row < self.firsrtDay+self.days) {

//点击的cell改变颜色

self.selectedDate = [ConvertDate ConvertDateWithYear:self.yearsNum months:self.monthsNum day:indexPath.row- self.firsrtDay+1];

NSDate *sectionDate = [NSCalendar date:_fromDate addMonth:indexPath.section];

NSDate *yyMMDDDate = [self dateYYMMConvertToYYMMDD:sectionDate withDay:indexPath.row + 1];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"yyyy-MM-dd"];

self.selectedDate = [formatter stringFromDate:yyMMDDDate];

DLog(@"indexPath.row:%ld",(long)indexPath.row);

_calendarModelNum = 1;

[self.calendarCollectionView reloadData];

if (self.sendDateBlock) {

self.sendDateBlock(self.selectedDate);

}

}

*/

你可能感兴趣的:(CalendarView 优化方法)