日历的签到一般有两个月,这个月上个月。
获得了两个数组 里面分别装了 本月和上月的天数。在结合这个月的第一天是星期几,就可以利用collectionView做出日历了。
- (void)getDate
{
//本月
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents *this = [calendar components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[[NSDate alloc] init]];
NSDate *thisMonDate = [calendar dateFromComponents:this];
//得到这个月天数
NSInteger dayNum = [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:thisMonDate].length;
//这个月的第一天是星期几
_dayfirst = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:thisMonDate];
NSString * str1 = [[NSString stringWithFormat:@"%@",[NSDate date]] substringToIndex:7];
NSInteger day = [[str1 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]*100;
_monthDay = [[NSMutableArray alloc]init];
//本月所以日期,拿出一个数组来存放这个月的天数,这里用对象,可以在日历里存很多信息,不仅仅是日期
for (int i = 0; i < dayNum; i++) {
Time * aa = [[Time alloc]init];
aa.create_time = [NSString stringWithFormat:@"%ld",(long)day+1+i];
[_monthDay addObject:aa];
}
//上个月
NSDateComponents *last = [calendar components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[[NSDate alloc] init]];
[last setMonth:[last month]-1];
NSString * havePervious = [[[[NSString stringWithFormat:@"%@",thisMonDate]substringToIndex:10]substringFromIndex:5]substringToIndex:2];
if ([havePervious isEqualToString:@"01"]) {
last = this;
}
NSDate *lastMonDate = [calendar dateFromComponents:last];//这里的日期其实是当月减去两个月的末尾日
//得到上个月天数
NSInteger perDayNum = [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:lastMonDate].length;
_perviousDay = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:lastMonDate];
NSString * str2 = [[NSString stringWithFormat:@"%@",lastMonDate] substringToIndex:7];
NSInteger perday = ([[str2 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]+1)*100;
_perviousMonth = [[NSMutableArray alloc]init];
//上月所有日期
for (int i = 0; i < perDayNum; i++) {
Time * aa = [[Time alloc]init];
aa.create_time = [NSString stringWithFormat:@"%ld",(long)perday+1+i];
[_perviousMonth addObject:aa];
}
}
collection的创建
_dayTitle = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
_countCollectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, KSCREEN_WIDTH, (KSCREEN_WIDTH/7.0-6)*6) collectionViewLayout:flowLayout];
_countCollectView.delegate = self;
_countCollectView.dataSource = self;
_countCollectView.backgroundColor = [UIColor clearColor];
[_countCollectView registerClass:[MyCalendarCell class] forCellWithReuseIdentifier:@"cellId"];
//这里第一行的星期之所以不用section是因为间距之间不好调,直接选用了42格,所以接下来的row需要减
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 42;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellId = @"cellId";
RdCalendarCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
if (cell == nil) {
cell = [[MyCalendarCell alloc]init];
}
NSInteger row = indexPath.row-6;
if (row>=_dex && indexPath.row <= (_dayArray.count+_dex+5)) {
if (_dayArray.count > 0) {
//找出今天的日期
NSString * day = [_dayArray[row-_dex]create_time];
cell.dayLabel.text = [day substringFromIndex:6];
cell.myImageView.image = IMAGEOF(@"");
if (_monthArr.count && row < _monthArr.count+_dex) {
RdCardTime * model = _monthArr[row-_dex];
if ([day integerValue]==_today) {
cell.dayLabel.backgroundColor = [UIColor BlueColor];
cell.dayLabel.textColor = [UIColor whiteColor];
cell.todayLabel.text = @"今天";
[self countInformation:_dayArray[row-_dex] :row-1 :model];
}else{
cell.dayLabel.backgroundColor = [UIColor clearColor];
cell.dayLabel.textColor = kTextColor;
cell.todayLabel.text = @"";
}
}
}
}else{
cell.dayLabel.text = @"";
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((KSCREEN_WIDTH/7.0-0.3)-0.6, (KSCREEN_WIDTH/7.0-6)-0.6);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//挑出可点击的日期,可点击相应的cell,然后进行一些数据的显示
NSInteger row = indexPath.row-6;
if (row>=_dex && indexPath.row <= (_monthArr.count+_dex+5)) {
}
}