Implement a simple calendar control on iOS

Source: Internet
Author: User

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
    1. @interface NSDate (wqcalendarlogic)

0. First you need to know how many days this month:

[CPP]View PlainCopy
    1. -(Nsuinteger) Numberofdaysincurrentmonth
    2. {
    3. //frequent calls to [Nscalendar Currentcalendar] may have performance issues
    4. return [[Nscalendar Currentcalendar] rangeofunit:nsdaycalendarunit inunit:nsmonthcalendarunit forDate:self].  Length
    5. }

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
  1. -(NSDate *) firstdayofcurrentmonth
  2. {
  3. NSDate *startdate = nil;
  4. BOOL OK = [[Nscalendar Currentcalendar] Rangeofunit:nsmonthcalendarunit startdate:&startdate Interval:NULL  Fordate:self];
  5. NSAssert1 (OK, @ "Failed to calculate the first day of themonth based on%@", self);
  6. return startdate;
  7. }
  8. -(Nsuinteger) weeklyordinality
  9. {
  10. return [[Nscalendar Currentcalendar] ordinalityofunit:nsdaycalendarunit inunit:nsweekcalendarunit forDate:self]  ;
  11. }

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
  1. -(Nsuinteger) Numberofweeksincurrentmonth
  2. {
  3. Nsuinteger weekday = [[self firstdayofcurrentmonth] weeklyordinality];
  4. Nsuinteger days = [self numberofdaysincurrentmonth];
  5. Nsuinteger weeks = 0;
  6. if (Weekday > 1) {
  7. Weeks + = 1, days-= (7-weekday + 1);
  8. }
  9. Weeks + = DAYS/7;
  10. Weeks + = (days% 7 > 0)? 1:0;
  11. return weeks;
  12. }


Here, you can tell how many rows are in one months and how many small squares are needed to show:

[CPP]View PlainCopy
    1. @interface Wqcalendartileview:uiview


These small squares are loaded with a large block:

[CPP]View PlainCopy
    1. @interface Wqcalendargridview:uiview
    2. @property (nonatomic, weak) id<wqcalendargridviewdatasource> DataSource;
    3. @property (nonatomic, weak) id<wqcalendargridviewdelegate> delegate;
    4. -(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
  1. @class Wqcalendargridview;
  2. @protocol Wqcalendargridviewdatasource <NSObject>
  3. @required
  4. -(Nsuinteger) Numberofrowsingridview: (Wqcalendargridview *) GridView;
  5. -(Wqcalendartileview *) GridView: (Wqcalendargridview *) GridView Tileviewforrow: (nsuinteger) Row column: (Nsuinteger) Column
  6. @optional
  7. -(CGFloat) Heightforrowingridview: (Wqcalendargridview *) GridView;
  8. @end
  9. @protocol wqcalendargridviewdelegate <NSObject>
  10. -(void) GridView: (Wqcalendargridview *) GridView Didselectatrow: (nsuinteger) Row column: (nsuinteger) column;
  11. @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
  1. #pragma mark-method to calculate days in previous, current and the following month.
  2. -(void) Calculatedaysinpreviousmonthwithdate: (NSDate *) Date
  3. {
  4. Nsuinteger weeklyordinality = [[Date firstdayofcurrentmonth] weeklyordinality];
  5. NSDate *dayinthepreviousmonth = [date Dayinthepreviousmonth];
  6. Nsuinteger Dayscount = [Dayinthepreviousmonth numberofdaysincurrentmonth];
  7. Nsuinteger partialdayscount = weeklyOrdinality-1;
  8. Nsdatecomponents *components = [Dayinthepreviousmonth ymdcomponents];
  9. Self.daysinpreviousmonth = [Nsmutablearray arraywithcapacity:partialdayscount];
  10. For (int i = dayscount-partialdayscount + 1; i < Dayscount + 1; ++i) {
  11. Wqcalendarday *calendarday = [Wqcalendarday calendarDayWithYear:components.year month:components.month day:i];
  12. [Self.daysinpreviousmonth Addobject:calendarday];
  13. [Self.calendardays Addobject:calendarday];
  14. }
  15. }
  16. -(void) Calculatedaysincurrentmonthwithdate: (NSDate *) Date
  17. {
  18. Nsuinteger Dayscount = [date Numberofdaysincurrentmonth];
  19. Nsdatecomponents *components = [date ymdcomponents];
  20. Self.daysincurrentmonth = [Nsmutablearray arraywithcapacity:dayscount];
  21. For (int i = 1; i < Dayscount + 1; ++i) {
  22. Wqcalendarday *calendarday = [Wqcalendarday calendarDayWithYear:components.year month:components.month day:i];
  23. [Self.daysincurrentmonth Addobject:calendarday];
  24. [Self.calendardays Addobject:calendarday];
  25. }
  26. }
  27. -(void) Calculatedaysinfollowingmonthwithdate: (NSDate *) Date
  28. {
  29. Nsuinteger weeklyordinality = [[Date lastdayofcurrentmonth] weeklyordinality];
  30. if (weeklyordinality = = 7) return;
  31. Nsuinteger partialdayscount = 7-weeklyordinality;
  32. Nsdatecomponents *components = [[Date dayinthefollowingmonth] ymdcomponents];
  33. Self.daysinfollowingmonth = [Nsmutablearray arraywithcapacity:partialdayscount];
  34. For (int i = 1; i < Partialdayscount + 1; ++i) {
  35. Wqcalendarday *calendarday = [Wqcalendarday calendarDayWithYear:components.year month:components.month day:i];
  36. [Self.daysinfollowingmonth Addobject:calendarday];
  37. [Self.calendardays Addobject:calendarday];
  38. }
  39. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.