Use NSCalendar and UICollectionView to customize the calendar, and implement sign-In display and custom calendar control sign-in

Source: Internet
Author: User

Use NSCalendar and UICollectionView to customize the calendar, and implement sign-In display and custom calendar control sign-in

Some time ago, a calendar can be signed in because of work needs to be implemented to record the implementation idea. The basic requirement here is: 1. display the user's sign-in status for a certain month, if you do not sign in, you can sign in within a certain time range. Others can only be checked. 2. The server will only return the timestamp array of the user's sign-In date and the time range that can be checked in. The remaining days will not be checked in. 3. The display is the same as that of a normal calendar. The above is the "May 6, 1234", and the following is the corresponding date. 4. You can switch to the month before the current date. As needed, the basic idea is to use a pageViewController as the basic controller to control the viewController that displays the sign-in status for a month. (Why is pageViewController used? You can switch the month according to the requirement. pageViewController is used for reuse. In addition, you may need to request server data every month and write network requests to viewController to better control the entire lifecycle) each viewController is a month. A custom calendarView is put on the top, and UICollectionView is used to implement the CalendarView. Each cell displays the date of the Day, and several States are defined to indicate that you can sign in and cannot sign in, check the status of the cell. This is probably the design of the interface. The difficulty is the logic and Data configuration of the calendar, determine the date of the current day and the DateComponents of the current month based on the timestamp returned by the server (to prevent users from cheating by modifying the system time, and use NSDateComponents to indicate convenient comparison for a specific month). Then, the calendar is displayed based on the dateComponents of the current month. Note that you need to find the number of days in the current month and the number of days in the first day of the current month !!! Obtain dateComponents: NSDateComponents * firstDayComponents = [[NSDateComponents alloc] init]; firstDayComponents on the first day of the month. year = monthComponents. year; firstDayComponents. month = monthComponents. month; firstDayComponents. day = 1; then you can use the NSDate * firstDay = [self. calendar dateFromComponents: firstDayComponents]; NSDateComponents * dateComponents = [self. calendar components: NSCalendarUnitYea R | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate: firstDay]; nsange range = [self. calendar rangeOfUnit: NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate: firstDay]; // specify the location of the day of the date in the month, so the length of the range is the number of days in the month, dateComponents. weekday indicates the day of the week on the first day of the month. Note that the firstWeekday and minimumDaysInFirstWeek attributes of NSCalendar are:-(NSCalendar *) calendar {
If (! _ Calendar ){
_ Calendar = [NSCalendar currentCalendar];
_ Calendar. firstWeekday = 2; // 1 indicates Sunday, 2 indicates Monday, and so on.
_ Calendar. minimumDaysInFirstWeek = 1; // indicates that if the first week of a month is less than this value, it is regarded as the last week of the previous month. If the value is equal to or equal to the value, it is regarded as the first week of the month.
}
Return _ calendar;} with this, you can calculate the number of blank cells before the first day of the month: self. frontBlankCount = dateComponents. weekday-self. calendar. firstWeekday; if (self. frontBlankCount <0 ){
// Here, because weekday is 1 on Sunday
Self. frontBlankCount + = 7;} then calculate the number of white spaces in the last month: for (int I = 0; I <self. frontBlankCount; I ++) {// This is to make the first place of each month blank as needed
[Self. dataArray addObject: @ ""];
}
For (int I = 0; I <range. length; I ++ ){
NSString * text = [NSString stringWithFormat: @ "% @", @ (I + 1)];
[Self. dataArray addObject: text];
}

NSInteger weeks = 0; // number of weeks per month
NSInteger aaa = self. dataArray. count/7;
NSInteger bbb = self. dataArray. count % 7;
If (bbb = 0 ){
Weeks = aaa;
} Else {
Weeks = aaa + 1;
} Self. backBlankCount = weeks * 7-self. dataArray. count; the calendar can be displayed completely here. The rest is the matching sign-in data: you only need a for loop to determine whether the calendar date is in the sign-in date returned by the server. If you need to determine whether the calendar date can be checked in, add a timestamp to determine the size. Finally, reloadData can be used ~~~ Demo on github: https://github.com/Phelthas/Demo_Calendar

Related Article

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.