iOS控件之日历
2016-02-25 16:45
411 查看
iOS没用日历控件,所以我们需要自己进行封装,博主是用的CollectionView封装的
其实难就难在如何处理数据,下面上代码
这里是我封装的CollectionView,继承UICollectionView
首先是.h文件
然后是.m文件
下面我们来调用封装好的控件
其实难就难在如何处理数据,下面上代码
这里是我封装的CollectionView,继承UICollectionView
首先是.h文件
#import <UIKit/UIKit.h> @interface THCCalendar : UICollectionView @property (nonatomic,assign)NSInteger year; @property (nonatomic,assign)NSInteger month; @property (nonatomic,assign)NSInteger day; - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout AndCellName:(NSString *)name AndCellID:(NSString *)CellID; -(void)LastMonthClick; -(void)NextbuttonClick; @end
然后是.m文件
#import "THCCalendar.h" #import "CollectionViewCell1.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface THCCalendar ()<UICollectionViewDelegate,UICollectionViewDataSource> /* ****用来存放天数的数组 */ @property (nonatomic,strong)NSArray * weekArray; @property (nonatomic,strong)NSMutableArray * dayArray; @property (nonatomic,strong)NSDateComponents * nowDate; @property (nonatomic,strong)NSCalendar *calendar; @property (nonatomic,strong)NSDate *lastMonthDate; @property (nonatomic,strong)NSDate *NowMonthfirst; @property (nonatomic,copy)NSString * CellId; @end @implementation THCCalendar //name是Cell的name CellID是cell的id - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout AndCellName:(NSString *)name AndCellID:(NSString *)CellID { self = [super initWithFrame:frame collectionViewLayout:layout]; if (self) { self.delegate = self; self.dataSource = self; [self registerNib:[UINib nibWithNibName:name bundle:nil] forCellWithReuseIdentifier:CellID]; self.backgroundColor = [UIColor whiteColor]; _CellId = CellID; [self initDataSource]; } return self; } //请求下一个月的数据 -(void)NextbuttonClick{ [_dayArray removeAllObjects]; if (_month == 12) { _month = 1; _year++; }else{ _month++; } [self setMydayArrayWithYear:_year AndMonth:_month AndDay:_day]; [self reloadData]; } //请求上一个月的数据 -(void)LastMonthClick{ [_dayArray removeAllObjects]; if (_month == 1) { _month = 12; _year--; }else{ _month--; } [self setMydayArrayWithYear:_year AndMonth:_month AndDay:_day]; [self reloadData]; } //初始化数据 -(void)initDataSource{ _weekArray = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"]; _dayArray = [[NSMutableArray alloc] init]; _calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; _nowDate = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:[NSDate date]]; _year = _nowDate.year; _month = _nowDate.month; _day = _nowDate.day; [self setMydayArrayWithYear:_year AndMonth:_month AndDay:_day];//给dayarray赋值 } -(NSDate*)setLastMonthWithYear:(NSInteger)year AndMonth:(NSInteger)month AndDay:(NSInteger)day{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate * date = nil; if (month != 1) { date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",year,month-1,day]]; }else{ date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%d-%ld",year,12,day]]; } return date; } //请求数据 -(void)setMydayArrayWithYear:(NSInteger)year AndMonth:(NSInteger)month AndDay:(NSInteger)day{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate * nowDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",year,month,day]]; NSRange dayRange = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:nowDate]; NSRange lastdayRange = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[self setLastMonthWithYear:year AndMonth:month AndDay:day]]; _NowMonthfirst = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%d",year,month,1]]; NSDateComponents * components = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:_NowMonthfirst]; NSDate * nextDay = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",year,month,dayRange.length]]; NSDateComponents * lastDay = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:nextDay]; for (NSInteger i = lastdayRange.length - components.weekday + 2; i <= lastdayRange.length; i++) { NSString * string = [NSString stringWithFormat:@"%ld",i]; [_dayArray addObject:string]; } for (NSInteger i = 1; i <= dayRange.length ; i++) { NSString * string = [NSString stringWithFormat:@"%ld",i]; [_dayArray addObject:string]; } for (NSInteger i = 1; i <= (7 - lastDay.weekday); i++) { NSString * string = [NSString stringWithFormat:@"%ld",i]; [_dayArray addObject:string]; } } //代理方法 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _dayArray.count; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((WIDTH-26)/7,(WIDTH-26)/7); } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ CollectionViewCell1 * cell = [collectionView dequeueReusableCellWithReuseIdentifier:_CellId forIndexPath:indexPath]; cell.cellLabel.text = _dayArray[indexPath.row]; cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.selectedBackgroundView.backgroundColor = [UIColor darkGrayColor]; return cell; } //调整Item的位置 使Item不紧挨着屏幕 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ //在原有基础上进行调整 上 左 下 右 return UIEdgeInsetsMake(1, 10, 0, 10); } //设置水平间距与竖直间距 默认为10 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 1; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 1; } @end
下面我们来调用封装好的控件
#import "ViewController.h" #import "THCCalendar.h" #import "CollectionViewCell1.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *nowMouth; @property (nonatomic,strong)THCCalendar * calendar; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //日历说白了就是一个CollectionView [self initCollectionView]; } -(void)initCollectionView{ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; //设置对齐方式 [layout setScrollDirection:UICollectionViewScrollDirectionVertical]; //cell间距 layout.minimumInteritemSpacing = 5.0f; //cell行距 layout.minimumLineSpacing = 1.0f; _calendar = [[THCCalendar alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 400) collectionViewLayout:layout AndCellName:@"CollectionViewCell1" AndCellID:@"collectionCell"] ; [self.view addSubview:_calendar]; _nowMouth.text = [NSString stringWithFormat:@"%ld年%ld月",_calendar.year,_calendar.month]; } - (IBAction)NextClick:(UIButton *)sender { [_calendar NextbuttonClick]; _nowMouth.text = [NSString stringWithFormat:@"%ld年%ld月",_calendar.year,_calendar.month]; } - (IBAction)LastClick:(UIButton *)sender { [_calendar LastMonthClick]; _nowMouth.text = [NSString stringWithFormat:@"%ld年%ld月",_calendar.year,_calendar.month]; }
相关文章推荐
- [IOS 开发] instancetype和id的区别
- iOS本地搜索(谓词)
- IOS发布的一些坑
- iOS开发--CocoaPods使用
- 详解 iOS navigationBar 的设置问题
- ios autolayout 使用总结
- #import与@class的区别
- [充电][ios]ios充电接口
- ios开发——iOS9 3D Touch 使用教程
- iOS开发之设置导航栏标题的字体大小和颜色
- iOS:手把手教你发布代码到CocoaPods(Trunk方式)
- iOS 创建带有Xib文件ViewController时 如何正确设置frame?
- iOS App集成Apple Pay教程(附示例代码)
- iOS 视频边下边播
- iOS 的 XMPPFramework 简介
- 内存管理与属性的参数
- AFNetworking施工例子
- 关于IOS多线程的好的技术博客
- iOS sizeClass设备对照表
- iOS-OC-AFNetworking上传和下载(回调下载进度)