我有这个代码:
- (void) setDataLabel{
for (int k = 0; k<31; k++){
[[lineSunday objectAtIndex:k] setAlpha:0.00];
[[arrayDay objectAtIndex:k] setTextColor:[UIColor whiteColor]];
}
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:10];
//NSLog(@"mese:%d", month);
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
for (int i = 0; i < 31; i++) {
NSTimeInterval seconds = 24*60*60 * i;
NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
NSDateComponents *weekdayComponents = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
NSString *strDate = [dateFormatter stringFromDate: date];
[[arrayDay objectAtIndex:i] setText:strDate];
if (weekday == 1) {
[[arrayDay objectAtIndex:i] setTextColor:[UIColor redColor]];
[[lineSunday objectAtIndex:i] setAlpha:1.00];
}
}
这段代码设置了 31 个带有月份天数的标签,一切正常,但我不明白为什么 10 月份有连续两个工作日;一个例子:在今年,这段代码在月末这样写:
....25 26 27 28 29 30 30
和 30 和 30 是红色的,但不应该是这样,应该是
....25 26 27 28 29 30 31
并且只有 30 个必须是红色的
为什么会发生?