在iOS上实现一个简单的日历控件

@interface NSDate (WQCalendarLogic)

0. 首先需要知道这个月有多少天：

- (NSUInteger)numberOfDaysInCurrentMonth
{
// 频繁调用 [NSCalendar currentCalendar] 可能存在性能问题
return [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self].length;
}

1. 确定这个月的第一天是星期几。这样就能知道给定月份的第一周有几天：

- (NSDate *)firstDayOfCurrentMonth
{
NSDate *startDate = nil;
BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self];
NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
return startDate;
}

- (NSUInteger)weeklyOrdinality
{
return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
}

2. 减去第一周的天数，剩余天数除以7，得到倍数和余数：

- (NSUInteger)numberOfWeeksInCurrentMonth
{
NSUInteger weekday = [[self firstDayOfCurrentMonth] weeklyOrdinality];

NSUInteger days = [self numberOfDaysInCurrentMonth];
NSUInteger weeks = 0;

if (weekday > 1) {
weeks += 1, days -= (7 - weekday + 1);
}

weeks += days / 7;
weeks += (days % 7 > 0) ? 1 : 0;

return weeks;
}

@interface WQCalendarTileView : UIView

@interface WQCalendarGridView : UIView

@property (nonatomic, weak) id<WQCalendarGridViewDataSource> dataSource;
@property (nonatomic, weak) id<WQCalendarGridViewDelegate> delegate;

- (void)reloadData;

@class WQCalendarGridView;

@protocol WQCalendarGridViewDataSource <NSObject>

@required

- (NSUInteger)numberOfRowsInGridView:(WQCalendarGridView *)gridView;

- (WQCalendarTileView *)gridView:(WQCalendarGridView *)gridView tileViewForRow:(NSUInteger)row column:(NSUInteger)column;

@optional

- (CGFloat)heightForRowInGridView:(WQCalendarGridView *)gridView;

@end

@protocol WQCalendarGridViewDelegate <NSObject>

- (void)gridView:(WQCalendarGridView *)gridView didSelectAtRow:(NSUInteger)row column:(NSUInteger)column;

@end

#pragma mark - method to calculate days in previous, current and the following month.

- (void)calculateDaysInPreviousMonthWithDate:(NSDate *)date
{
NSUInteger weeklyOrdinality = [[date firstDayOfCurrentMonth] weeklyOrdinality];
NSDate *dayInThePreviousMonth = [date dayInThePreviousMonth];

NSUInteger daysCount = [dayInThePreviousMonth numberOfDaysInCurrentMonth];
NSUInteger partialDaysCount = weeklyOrdinality - 1;

NSDateComponents *components = [dayInThePreviousMonth YMDComponents];

self.daysInPreviousMonth = [NSMutableArray arrayWithCapacity:partialDaysCount];
for (int i = daysCount - partialDaysCount + 1; i < daysCount + 1; ++i) {
WQCalendarDay *calendarDay = [WQCalendarDay calendarDayWithYear:components.year month:components.month day:i];
}
}

- (void)calculateDaysInCurrentMonthWithDate:(NSDate *)date
{
NSUInteger daysCount = [date numberOfDaysInCurrentMonth];
NSDateComponents *components = [date YMDComponents];

self.daysInCurrentMonth = [NSMutableArray arrayWithCapacity:daysCount];
for (int i = 1; i < daysCount + 1; ++i) {
WQCalendarDay *calendarDay = [WQCalendarDay calendarDayWithYear:components.year month:components.month day:i];
}
}

- (void)calculateDaysInFollowingMonthWithDate:(NSDate *)date
{
NSUInteger weeklyOrdinality = [[date lastDayOfCurrentMonth] weeklyOrdinality];
if (weeklyOrdinality == 7) return ;

NSUInteger partialDaysCount = 7 - weeklyOrdinality;
NSDateComponents *components = [[date dayInTheFollowingMonth] YMDComponents];

self.daysInFollowingMonth = [NSMutableArray arrayWithCapacity:partialDaysCount];
for (int i = 1; i < partialDaysCount + 1; ++i) {
WQCalendarDay *calendarDay = [WQCalendarDay calendarDayWithYear:components.year month:components.month day:i];
}
}

移动端仿ios日历插件

2017-06-03 17:38:19

iOS无限滚动，实现ios原生日历

2015-10-28 15:56:45

iOS之自定义简单的日历控件

2017-04-10 14:27:26

iOS日历视图 FSCalendar的使用

2016-11-30 16:24:22

ios 横向日历控件的使用

2016-12-30 11:43:51

iOS 【一行代码集成一款精致的第三方OC日历——WZYCalendar】

2016-12-25 18:23:09

iOS 自定义日期和数据源选择控件

2017-02-08 12:27:41

iOS的日历控件开源代码

2013-07-30 15:31:57

iOS控件之日历

2016-02-25 16:45:58

仿IOS日期控件(增加上下午，时分选择)

2015年06月01日 1.44MB 下载