您的位置:首页 > 移动开发 > IOS开发

iOS控件之日历

2016-02-25 16:45 411 查看
iOS没用日历控件,所以我们需要自己进行封装,博主是用的CollectionView封装的

其实难就难在如何处理数据,下面上代码

这里是我封装的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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: