2

Hi all i am working with FSCalandar in my project. Everything is working fine but i am not able change calendar date color for the date which will entered by user. Basically i want to add the functionality of present and absent in my project using FSCalander. i already called fscalanderappearance delegate method.Please look at my code.

This is .h file

@interface CalTableCellTableViewCell : UITableViewCell <FSCalendarDelegate,      FSCalendarDataSource, FSCalendarDelegateAppearance>
   {
NSDate *myDate;
NSDate *EndDate;
NSMutableArray *array;
 }

  @property (weak , nonatomic) FSCalendar *calendar;
   @property (weak, nonatomic) IBOutlet NSLayoutConstraint *calendarHeightConstraint;

   @property (weak, nonatomic) IBOutlet UIView *calview;
   @property (strong, nonatomic) NSDate *selectedDate;

   @property (strong, nonatomic) NSDictionary *fillSelectionColors;
   @property (strong, nonatomic) NSDictionary *fillDefaultColors;
   @property (strong, nonatomic) NSDictionary *borderDefaultColors;
   @property (strong, nonatomic) NSDictionary *borderSelectionColors0;

   @property (strong, nonatomic) NSArray *datesWithEvent;
   @property (strong, nonatomic) NSArray *datesWithMultipleEvents;


   @end

This is .m file.

#import "CalTableCellTableViewCell.h"

    @implementation CalTableCellTableViewCell

     - (void)awakeFromNib {
[super awakeFromNib];
// Initialization code

myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"];
NSLog(@"mydate %@", myDate);



EndDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"EndDateKey"];

FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, 250, 150)];
calendar.dataSource = self;
calendar.delegate = self;

[self.contentView addSubview:calendar];
self.calendar = calendar;
_calendar.scrollDirection = FSCalendarScrollDirectionVertical;
    calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"d.M.yyyy";
NSString *string = [formatter stringFromDate:[NSDate date]];

NSLog(@"current date %@", string);
_calendar.appearance.selectionColor = [UIColor blueColor];

_calendar.appearance.todayColor = [UIColor greenColor];
_calendar.appearance.cellShape = FSCalendarCellStyleRectangle;



NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
NSArray *testarray = [userDef objectForKey:@"name"];

array = [[NSMutableArray alloc]initWithArray:testarray];
NSLog(@"arraydata %@", array);


self.datesWithEvent = @[@"2016-09-03",
                        @"2015-10-06",
                        @"2015-10-12",
                        @"2015-10-25"];

self.datesWithMultipleEvents = @[@"2015-10-08",
                                 @"2015-10-16",
                                 @"2015-10-20",
                                 @"2015-10-28"];


  }



  - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
    }





   - (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
   {
_calendarHeightConstraint.constant = CGRectGetHeight(bounds);
[self.contentView layoutIfNeeded];
    }

   - (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
    {


return myDate;


    }

   - (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
  {
return EndDate;


   }

     #pragma mark - <FSCalendarDelegateAppearance>


  - ( UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date;
    {
NSString *datestring = [calendar stringFromDate:myDate format:@"yyyy-MM-dd"];

if ([_datesWithEvent containsObject:datestring]) {

    NSLog(@"test");

    return calendar.appearance.borderDefaultColor = [UIColor redColor];

}

else if ([_datesWithMultipleEvents containsObject:datestring])
{
    NSLog(@"testing");
}



  return nil;

    }

Please help if anyone go with this problem. thanks. Delegates method not called.

1 Answers1

1

You can apply FSCalendar Data, Delegate & FSCalendarDelegateAppearance to the TableViewController or to the ViewController that contains TableView instead of implementing it in UITableViewCell. Then override the method titleDefaultColorForDate as shown below. It works for me.

-(UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date{

   self.formatter = [[NSDateFormatter alloc] init];
   self.formatter.dateFormat = @"yyyy/MM/dd";
   NSString *CurrentDate = [self.formatter stringFromDate:date];
   NSString *Today = [self.formatter stringFromDate:[_Calendar today]];

   //_bookedDates are array of specific dates
   if([_bookedDates containsObject:CurrentDate])
   //if booking dates contains currentdate change the color of the title
    return [UIColor whiteColor];
   else
    return appearance.titlePlaceholderColor;
}
Dinesh M
  • 71
  • 1
  • 9