iOS自定义日历控件的简单实现过程

2020-01-18 16:17:07王振洲


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

 LYWCollectionViewCell *cell = [self collectionView:_collectionView cellForItemAtIndexPath:indexPath];

 NSDate *currentDate = [[NSDate alloc]init];

 //打印当前日期

 if (![cell.dateLable.text isEqualToString:@""]) {

 NSInteger year = ([currentDate month:currentDate] + indexPath.section)/12 + 2016;

 NSInteger mounth = ([currentDate month:currentDate] + indexPath.section)%12;

 NSInteger day = [cell.dateLable.text intValue];

 NSLog(@"%ld年%02ld月%02ld日",year,mounth,day);

 }

 //排除空值cell

 //获取月份

 NSInteger mounth = ([currentDate month:currentDate] + indexPath.section) % 12 == 0 ? 12 : ([currentDate month:currentDate] + indexPath.section)%12;

 NSDateComponents *components = [[NSDateComponents alloc]init];

 components.month = mounth;

 components.year = 2016 + mounth/12;

 components.day = 1;

 NSCalendar *calendar = [NSCalendar currentCalendar];

 NSDate *nextDate = [calendar dateFromComponents:components];

 //获取该月第一天星期几

 NSInteger firstDayInThisMounth = [nextDate firstWeekdayInThisMonth:nextDate];

 //该月的有多少天daysInThisMounth

 NSInteger daysInThisMounth = [nextDate totaldaysInMonth:nextDate];

 if ((indexPath.row < firstDayInThisMounth || indexPath.row > daysInThisMounth + firstDayInThisMounth - 1)){

 //如果点击空表格则单击无效

 [collectionView cellForItemAtIndexPath:indexPath].userInteractionEnabled = NO;

 [collectionView reloadData];

 }

}

最后展示很烂的效果图:

iOS,日历控件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。