最近开发需要,所以自己设计了一个日历视图,主要用到了UICollectionView,以及UICalendar和NSDate部分,总体来说还是比较容易的,毕竟我是借鉴(抄的),这里我是把日历封装到一个collectionView中,这样比较方面使用。那么简单的总结吧~
目前效果是这样的,但是还不是无限滚动的,我的思路是当滑到倒数第二个section的时候,sectionNumber+10,这样循环下去,但是感觉不好,所以还是期待其他思路,也希望有好的想法能一起分享~
首先先介绍下Model,这里所有的数据都是在Model中进行的处理。我们这使用类目对Date添加了一系列类方法。
/** 获取日 */ + (NSInteger)day:(NSDate *)date; /** 获取农历日 */ + (NSString *)chineseDay:(NSDate *)date; /** 获取月 */ + (NSInteger)month:(NSDate *)date; /** 获取农历月 */ + (NSString *)chineseMonth:(NSDate *)date; /** 获取年 */ + (NSInteger)year:(NSDate *)date; /** 获取农历年 */ + (NSString *)chineseYear:(NSDate *)date; /** Date转字符串 */ + (NSString * )theTargetDateConversionStr:(NSDate * )date; /** 字符串转Date */ + (NSDate* )theTargetStringConversionDate:(NSString *)str; /** 获取当前月第一天周几 */ + (NSInteger)firstWeekdayInThisMonth:(NSDate *)date; /** 获取当前月份有多少天 */ + (NSInteger)totaldaysInMonth:(NSDate *)date; /** 获取目标Date几天后的Date */ + (NSDate *)getDateSinceDate:(NSDate *)date AfterDays:(NSInteger)days; /** 几个月之后的1号Date */ + (NSDate *)getAMonthframDate:(NSDate*)date months:(NSInteger)number; /** 与现在时间比较 */ + (int)compareCurrentDateBydate:(NSDate *)date; 这里经常用的有获取当前月的第一天是周几,当前月份有多少天和几个月后1号Date。因为知道了这三个,你就可以知道在section中一个月的开头是在第几个row,一共有多少个row,以及接下来的月的这些信息。 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // 根据当前Date获取接下来几个月的1号Date NSDate * amonthOf1_Date = [NSDate getAMonthframDate:_currentDate months:section]; // 月份的总天数加上1号所在的周几为 section所显示row的总数 return [NSDate totaldaysInMonth:amonthOf1_Date] + [NSDate firstWeekdayInThisMonth:amonthOf1_Date]; } 这里是cell中的数据及显示 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CalendarCell *cell = (CalendarCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdenter forIndexPath:indexPath]; // section对应月的1号Date对象 NSDate * amountOf1_Date =[NSDate getAMonthframDate:_currentDate months:indexPath.section]; // 1号对应的周几 NSInteger number = [NSDate firstWeekdayInThisMonth:amountOf1_Date]; // 在1号之前的cell 要隐藏 不显示 if (indexPath.row >= number) { // row 对应的Date cell.date = [NSDate getDateSinceDate:amountOf1_Date AfterDays:(indexPath.row - number)]; } else{ cell.hidden = YES; } return cell; } 这里是Section HeaderView的数据及显示 -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (kind == UICollectionElementKindSectionHeader) { CalendarSectionHeaderView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:calendarHeaderIdenter forIndexPath:indexPath]; //当前月 section月后的1号 NSDate *firstDate = [NSDate getAMonthframDate:_currentDate months:indexPath.section]; // 获取年 NSInteger year = [NSDate year:firstDate]; // 获取月 NSInteger month = [NSDate month:firstDate]; // 获取农历年 NSString *chineseMonth = [NSDate chineseMonth:firstDate]; // 获取农历月 NSString *chineseYear = [NSDate chineseYear:firstDate]; headerView.yearerLabel.text= [NSString stringWithFormat:@"%ld年%ld月 %@年%@",(long)year, (long)month, chineseYear, chineseMonth]; return headerView; } else { UICollectionReusableView * footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footView" forIndexPath:indexPath]; return footView; }; }总体来说就这么多,更多的功能可以自己进行添加,样式也可以自己进行调整。代码demo 在下面~
Demo地址
参考链接iOS 获取农历方法
iOS日历、NSCalendar初探(基础篇)
