-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonthPickerView.m
80 lines (67 loc) · 2.18 KB
/
MonthPickerView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// MonthPickerView.m
// MonthPicker
//
// Created by Gustavo Paris on 22/04/2020.
// Copyright © 2020 Gustavo Paris. All rights reserved.
//
#import "MonthPickerView.h"
#define DEFAULT_SIZE 204
@interface MonthPickerView2() <UIPickerViewDataSource, UIPickerViewDelegate>
@end
@implementation MonthPickerView2
NSMutableArray *monthts;
NSMutableArray *years;
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.delegate = self;
NSDate *date = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSCalendarUnitMonth|NSCalendarUnitYear) fromDate:date];
NSInteger currentMonth = [dateComponents month];
NSInteger currentYear = [dateComponents year];
years = [NSMutableArray array];
for(NSInteger i = currentYear - DEFAULT_SIZE; i <= currentYear + DEFAULT_SIZE; i ++) {
[years addObject:@(i)];
}
monthts = [NSMutableArray array];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
for(NSInteger i = 0; i < 12; i ++){
[monthts addObject:[[df monthSymbols] objectAtIndex:(i)]];
}
[self selectRow:(DEFAULT_SIZE / 2) - 7 + currentMonth inComponent:0 animated:YES];
[self selectRow:DEFAULT_SIZE inComponent:1 animated:YES];
}
return self;
}
// row titles
- (NSString *)pickerView:(nonnull UIPickerView *) pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0:
return [NSString stringWithFormat:@"%@", monthts[row % 12]];
break;
case 1:
return [NSString stringWithFormat:@"%@", years[row]];
break;
default:
return nil;
}
}
// number of columns
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
return 2;
}
// number of rows
- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
switch (component) {
case 0:
return DEFAULT_SIZE;
case 1:
return [years count];
break;
default:
return 0;
}
}
@end