如何更改特定日期的FSCalandar日期颜色。?

时间:2016-09-13 11:47:34

标签: ios objective-c fscalendar

  

大家好我在我的项目中与FSCalandar合作。一切正常,但我无法更改用户输入日期的日历日期颜色。基本上我想使用FSCalander在我的项目中添加当前和缺席的功能。我已经调用了fscalanderappearance委托方法。请查看我的代码。

     

这是.h文件

@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
  

这是.m文件。

#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;

    }
  

如果有人解决这个问题,请帮忙。谢谢。委托方法未调用。

1 个答案:

答案 0 :(得分:1)

您可以申请FSCalendar数据,代表和FSCalendarDelegateAppearance到TableViewController或包含ViewController的{​​{1}},而不是在TableView中实现它。然后覆盖方法 UITableViewCell ,如下所示。它对我有用。

titleDefaultColorForDate