相信大家項目中或多或少都有日歷這一塊的內容吧,公司作為教育行業的軟件公司,當然也是一定有的.
最近被日歷這一塊的內容弄得很頭疼啊,改來改去的,不過還是學到了很多東西,至少FSCalendar的使用基本還是熟悉了很多...
1.下面記錄一下:
- (FSCalendar *)calendar { if (!_calendar) { //日歷控件初始化 _calendar = [[FSCalendar alloc] init];
// 設置代理
_calendar.delegate = self; _calendar.dataSource = self; _calendar.firstWeekday = 2; //設置周一為第一天 _calendar.appearance.weekdayTextColor = [UIColor blackColor]; _calendar.appearance.weekdayFont = FONT_18; _calendar.appearance.headerTitleColor = [UIColor darkGrayColor]; _calendar.appearance.titleDefaultColor = [UIColor darkGrayColor]; _calendar.appearance.titleFont = FONT_18; // _calendar.appearance.subtitleDefaultColor = [UIColor greenColor]; _calendar.appearance.eventDefaultColor = [UIColor lightGrayColor]; _calendar.appearance.eventSelectionColor = [UIColor lightGrayColor]; _calendar.appearance.selectionColor = [UIColor redColor]; _calendar.appearance.headerDateFormat = @"yyyy年MM月"; _calendar.appearance.todayColor = [UIColor clearColor]; _calendar.appearance.titleTodayColor = [UIColor lightGrayColor]; _calendar.appearance.borderRadius = 1.0; // 設置當前選擇是圓形,0.0是正方形 _calendar.appearance.headerMinimumDissolvedAlpha = 0.0; _calendar.backgroundColor = [UIColor whiteColor]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設置為中文 _calendar.locale = locale; // 設置周次是中文顯示 // _calendar.headerHeight = 0.0f; // 當不顯示頭的時候設置 _calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesSingleUpperCase; // 設置周次為一,二 [_calendar selectDate:[NSDate date]]; // 設置默認選中日期是今天 // 設置不能翻頁 // _calendar.pagingEnabled = NO; // _calendar.scrollEnabled = NO; _calendar.placeholderType = FSCalendarPlaceholderTypeFillHeadTail; //月份模式時,只顯示當前月份 } return _calendar; }
placeholderType屬性需要特別提一下: 這個是月份模式時,只顯示當前月份,以前不知道這個屬性,因為框架沒有更新,測試提出需要不展示六行,日歷只需要展示五行.
2.下面介紹下我項目中用到的calendar的代理方法:
#pragma mark - FSCalendarDelegate // 設置五行顯示時的calendar布局 - (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated { [UIView animateWithDuration:.3 animations:^{ calendar.frame = (CGRect){calendar.frame.origin,bounds.size}; self.myTableView.frame = CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT); ScheduleHeaderView *headerView = (ScheduleHeaderView *)[self.myTableView headerViewForSection:0]; headerView.frame = calendar.frame; [self.myTableView reloadData]; } completion:^(BOOL finished) { }]; NSLog(@"0---%f",calendar.frame.origin.y); NSLog(@"0---%f",self.myTableView.frame.origin.y); } // 對有事件的顯示一個點,默認是顯示三個點 - (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd"; if ([self.datesWithEvent containsObject:[dateFormatter stringFromDate:date]]) { return 1; } return 0; } - (void)calendarCurrentPageDidChange:(FSCalendar *)calendar { NSLog(@"calendar.currentPage-- %@",calendar.currentPage); //日歷翻頁時記錄第一天 // NSDate *changeDate = [calendar tomorrowOfDate:calendar.currentPage]; NSDate *changeDate = calendar.currentPage; dateStr = [calendar stringFromDate:changeDate format:@"yyyyMMdd"]; //有事件的日期的年和月 NSDateFormatter *formatterYearAndMonth = [[NSDateFormatter alloc] init]; formatterYearAndMonth.dateFormat = @"yyyy-MM"; dateYearAndMonthStr = [formatterYearAndMonth stringFromDate:changeDate]; // 當日 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *todayDateYearAndMonthStr = [formatterYearAndMonth stringFromDate: [NSDate date]]; // 設置切換到當前月份的時候,顯示的是當天的日程 if ([[dateYearAndMonthStr substringWithRange:NSMakeRange(5, 2)] isEqualToString:[todayDateYearAndMonthStr substringWithRange:NSMakeRange(5, 2)]]) { dateStr = [calendar stringFromDate:[NSDate date] format:@"yyyyMMdd"]; } [self loadData]; } - (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date { NSLog(@"did select date %@",[calendar stringFromDate:date format:@"yyyy/MM/dd"]); dateStr = [calendar stringFromDate:date format:@"yyyyMMdd"]; NSLog(@"---%@",dateStr); [self loadData]; } - (BOOL)calendar:(FSCalendar *)calendar hasEventForDate:(NSDate *)date{ return [_datesWithEvent containsObject:[calendar stringFromDate:date format:@"yyyy-MM-dd"]]; }
3.剛開始項目中直接將Calendar作為表視圖的頭視圖,經過測試發現每次切換月份的時候因為改變的calendar的實際高度(因為每月不一定顯示是五行或者是六行),再刷新表視圖會導致表視圖會"閃跳"現象,后面直接將calendar作為表視圖的第一個section的headerView就解決了.
#pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return section == 0 ? 0 : self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { ScheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:MyScheduleCellIdentify forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (self.dataArray.count > 0) { cell.myApplyModel = self.dataArray[indexPath.row]; } return cell; } else { return nil; } } #pragma mark - UITableViewDelegate - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 300)]; [view addSubview:self.calendar]; self.calendar.frame = CGRectMake(0, 0, K_SCREEN_WIDTH, 300); return view; } else { ScheduleHeaderView *sectionHeaderView = [[ScheduleHeaderView alloc]initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 44)]; sectionHeaderView.titleLabel.text = @"今日會議"; sectionHeaderView.titleLabel.font = FONT_18; sectionHeaderView.badgeLabel.text = [NSString stringWithFormat:@"%ld",(unsigned long)self.dataArray.count]; return sectionHeaderView; } return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return self.calendar.frame.size.height ? : 300; } else { return 44; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.001f; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { return [tableView fd_heightForCellWithIdentifier:MyScheduleCellIdentify configuration:^(ScheduleCell *cell) { cell.myApplyModel = self.dataArray[indexPath.row]; }]; } else { return 0; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
總結:
1.剛開始覺得一個Calendar展示沒啥問題,畢竟有成熟的第三方FSCalendar嘛,實際動手才發現很多問題需要注意;
2.完成項目的一個功能的時候,不要想着有第三方的支持,直接用就好了,實際上還是要多看看作者的demo,熟悉框架的實現才能很好的用到項目中;
3.遇到問題,多想想可能造成的原因,多嘗試用不同的方法實現,說不定就解決了當前的bug問題.
4.空閑時間還是要多學學優秀的第三方框架,不能只停留在用的層面,想想實現和調用的原理,為什么要這個屬性,為什么要實現這個方法,為什么要調用這個代理.
多學習,愛學習的孩紙運氣不會太差,加油!!
