2

大家好,我在我的项目中与 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;

    }

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

4

1 回答 1

1

您可以将 FSCalendar Data、Delegate 和 FSCalendarDelegateAppearance 应用于包含的TableViewController或 ,而不是在. 然后重写该方法 ,如下所示。这个对我有用。ViewControllerTableViewUITableViewCelltitleDefaultColorForDate

-(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;
}
于 2017-05-09T12:31:07.313 回答