iOS:Tapku日历库 – 允许select当前月份的多个date
我使用Tapku库进行日历实施。 我可以看到有一种方法可以为预定义的开始date和结束date添加标记 ,但是我希望允许用户从当前月份select/取消select任意数量的date,并且希望为每个操作生成事件。
此外,我已经closures了月份导航function,通过返回nil左右箭头来显示当前月份,但无法删除前几个月和下个月的事件。 我仍然可以select上个月的第31天导航到上个月,或者select下个月的第1个导航到下个月。 我可以限制dateselect只有当前月吗?
谢谢。
触摸在TKCalendarMonthView.m
中通过以下方法处理:
- (void) reactToTouch:(UITouch*)touch down:(BOOL)down
看第563行的块:
if(portion == 1) { selectedDay = day; selectedPortion = portion; [target performSelector:action withObject:[NSArray arrayWithObject:[NSNumber numberWithInt:day]]]; } else if(down) { // this is the important part for you. // ignore it by adding a return here (or remove the following three lines) return; [target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]]; selectedDay = day; selectedPortion = portion; }
select/取消select也许不能按照您的预期工作。 它不是像setDateSelected
和setDateDeselected
..而是有一个单一的UIImageView*
,它代表select的状态。 而这一观点正在转向当前的位置。 你可以在代码中searchself.selectedImageView
,看看发生了什么。
所以引入多重dateselect并不那么容易。 该体系结构不是为此而构build的。
在TKCalendarMonthView
有一个方法名称
-(void) reactToTouch:(UITouch*)touch down:(BOOL)down
在该方法中注释这一行
[target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]];
这不会让你改变一个月。 您可以将所有选定date存储在一个数组中,并将所有值传入
- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate
上面的方法是用来放瓷砖,但如果你想select图像,那么你可以用瓷砖图像replace它
你也可以试试这个代码:
您可以通过首先将dateinput到数组中来完成此操作。 代码是这样的。
- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d { NSLog(@"selected Date IS - %@",inDate); [myArray addObject:d]; for (id entry in myArray) { if (inDate == nil && outDate == nil) { inDate = d; outDate = d; } if ([d compare:inDate] == NSOrderedAscending) { inDate = d; } if ([d compare:outDate] == NSOrderedDescending) { outDate = d; } d = nil; }
}
之后,您必须使用button点击操作,您可以在这两个date之间selectdate。 代码是:
- (IBAction)goBtn:(id)sender { NSLog(@"startDate is: %@",inDate); NSLog(@"endDate is: %@",outDate); [calendar reload]; inDate = nil; outDate = nil; }
}
然后在一个委托方法中,您只需创build一个包含这两个date之间的所有date的数组。 它会在点击button之后被调用。 代码是:
- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate { //*********** NSMutableArray *tempData = [[NSMutableArray alloc] init]; NSDate *nextDate; for ( nextDate = inDate ; [nextDate compare:outDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) { // use date NSLog(@"%@",nextDate); [tempData addObject:[NSString stringWithFormat:@"%@",nextDate]]; } [tempData addObject:[NSString stringWithFormat:@"%@",outDate]]; //*********** NSMutableArray *marks = [NSMutableArray array]; NSCalendar *cal = [NSCalendar currentCalendar]; [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:startDate]; NSDate *d = [cal dateFromComponents:comp]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:1]; while (YES) { if ([d compare:lastDate] == NSOrderedDescending) { break; } if ([tempData containsObject:[d description]]) { [marks addObject:[NSNumber numberWithBool:YES]]; } else { [marks addObject:[NSNumber numberWithBool:NO]]; } d = [cal dateByAddingComponents:offsetComponents toDate:d options:0]; } return [NSArray arrayWithArray:marks];
}
我希望,这对你有帮助。 如果您遇到任何问题,请告诉我。