iOS: Tapku calendar library - allow selecting multiple dates for current month

拥有回忆 提交于 2019-11-29 05:22:38

The touches are handled in TKCalendarMonthView.m in the following method:

- (void) reactToTouch:(UITouch*)touch down:(BOOL)down

look at the block at row 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;
}

The selecting/deselecting perhaps doesn't work as you expect. It's not like setDateSelected and setDateDeselected.. instead there is a single UIImageView*, which represents the selected state. And that view is moved around to the current position. You can search for self.selectedImageView in the code to see, what is happening.

So its not that easy to introduce multiple-date-selection. The architecture isn't built for that.

In TKCalendarMonthView there is a method name

-(void) reactToTouch:(UITouch*)touch down:(BOOL)down 

in that method comment this line

[target performSelector:action withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:day],[NSNumber numberWithInt:portion],nil]];

this wont allow you to change month. You can store all selected date in an array and pass all the values in

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate 

The above method is used to put tiles but if u want selection image then u can replace it with tile image

You can also try this code:

You can do this by first entering the dates in to an array. code for this is.

- (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;
}

}

After this you have to use a button click action by which you can make the dates selected between these two dates. Code for it is:

 - (IBAction)goBtn:(id)sender
  {
NSLog(@"startDate is: %@",inDate);
NSLog(@"endDate is: %@",outDate);

[calendar reload];
inDate = nil;
outDate = nil;

}

}

Then in one delegate method you just have to make an array containing all the dates between these two dates. It will be called just after the button click. Code for it is:

 - (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];

}

I hope, this helped you. Please let me know if you face any problem.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!