Transferred from: http://blog.csdn.net/jasonblog/article/details/21977481
Recently need to write an interactive little dt of the calendar control, the specific interactive details are omitted here is not a table.
However, the complexity of the control, but also by the basic parts of the assembly, the most basic here is the calendar control.
First:
You can see that the calendar control is made up of small squares, each row has 7 small squares, respectively, the week of Sunday to Saturday.
Given a month, we first need to know how many weeks this month. So how do you determine how many weeks are there in one months?
I think so, on the nsdate to do the expansion:
[CPP]View PlainCopy
- @interface NSDate (wqcalendarlogic)
0. First you need to know how many days this month:
[CPP]View PlainCopy
- -(Nsuinteger) Numberofdaysincurrentmonth
- {
- //frequent calls to [Nscalendar Currentcalendar] may have performance issues
- return [[Nscalendar Currentcalendar] rangeofunit:nsdaycalendarunit inunit:nsmonthcalendarunit forDate:self]. Length
- }
1. Make sure the first day of the month is the week. This allows you to know the first week of a given month for a few days:
[CPP]View PlainCopy
- -(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 themonth based on%@", self);
- return startdate;
- }
- -(Nsuinteger) weeklyordinality
- {
- return [[Nscalendar Currentcalendar] ordinalityofunit:nsdaycalendarunit inunit:nsweekcalendarunit forDate:self] ;
- }
2. Subtract the number of days from the first week and the remaining days divided by 7 to get multiples and remainder:
[CPP]View PlainCopy
- -(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;
- }
Here, you can tell how many rows are in one months and how many small squares are needed to show:
[CPP]View PlainCopy
- @interface Wqcalendartileview:uiview
These small squares are loaded with a large block:
[CPP]View PlainCopy
- @interface Wqcalendargridview:uiview
- @property (nonatomic, weak) id<wqcalendargridviewdatasource> DataSource;
- @property (nonatomic, weak) id<wqcalendargridviewdelegate> delegate;
- -(void) reloaddata;
Similar to UITableView, the layout begins when Wqcalendargridview calls the Reloaddata interface. The information required for the layout is provided by DataSource and delegate:
[CPP]View PlainCopy
- @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
The height of each line, Heightforrow, I prefer to provide by DataSource:)
The first DataSource method above can be calculated, the second DataSource method needs to configure each tile, such as the non-current month in the gray display, then we need to know the current month display contains the last month of the remainder, and the beginning of the next month:
[CPP]View PlainCopy
- #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];
- [Self.daysinpreviousmonth Addobject:calendarday];
- [Self.calendardays Addobject:calendarday];
- }
- }
- -(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];
- [Self.daysincurrentmonth Addobject:calendarday];
- [Self.calendardays Addobject:calendarday];
- }
- }
- -(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];
- [Self.daysinfollowingmonth Addobject:calendarday];
- [Self.calendardays Addobject:calendarday];
- }
- }
In this way, the calendar control for a given month can be successfully displayed. Recently, the project is busy, easy to remember an article:)
Implement a simple calendar control on iOS