SelectCheckDateCell.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // SelectCheckDateCell.m
  3. // DFCalendar
  4. //
  5. // Created by Macsyf on 16/12/7.
  6. // Copyright © 2016年 ZhouDeFa. All rights reserved.
  7. //
  8. #import "SelectCheckDateCell.h"
  9. #import "DayCell.h"
  10. @interface SelectCheckDateCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  11. @property(nonatomic,strong)UICollectionView *collectionView;
  12. @property(nonatomic,strong)UILabel *dateLabel;
  13. @property(nonatomic,strong)NSMutableArray *dataArray;
  14. @property(nonatomic,assign)NSInteger count;
  15. @property(nonatomic,assign)BOOL isHaveToday;
  16. @end
  17. @implementation SelectCheckDateCell
  18. -(NSMutableArray *)dataArray
  19. {
  20. if (!_dataArray) {
  21. _dataArray = [NSMutableArray array ];
  22. }
  23. return _dataArray;
  24. }
  25. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  26. {
  27. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  28. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 9)];
  29. lineView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
  30. [self addSubview:lineView];
  31. _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 48 )];
  32. _dateLabel.textAlignment = NSTextAlignmentCenter;
  33. _dateLabel.font = [UIFont systemFontOfSize:20];
  34. [self addSubview:_dateLabel];
  35. UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 59, [UIScreen mainScreen].bounds.size.width, 1)];
  36. lineView1.backgroundColor = [UIColor lightGrayColor];
  37. // [self addSubview:lineView1];
  38. _count = 0;
  39. [self addSubview:self.collectionView];
  40. }
  41. return self;
  42. }
  43. -(UICollectionView *)collectionView
  44. {
  45. if (!_collectionView) {
  46. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  47. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, 500) collectionViewLayout:flowLayout];
  48. _collectionView.dataSource = self;
  49. _collectionView.delegate = self;
  50. _collectionView.bounces = NO;
  51. _collectionView.scrollEnabled = NO;
  52. // _collectionView.backgroundColor = [UIColor whiteColor];
  53. _collectionView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
  54. [_collectionView registerNib:[UINib nibWithNibName:@"DayCell" bundle:nil] forCellWithReuseIdentifier:@"DayCell"];
  55. }
  56. return _collectionView;
  57. }
  58. -(void)fullCellWithModel:(MouthModel *)model
  59. {
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. CGRect frame = self.collectionView.frame;
  62. frame.size.height = model.cellHight - 60;
  63. self.collectionView.frame = frame;
  64. });
  65. self.dateLabel.text = [NSString stringWithFormat:@"%04ld年%02ld月",(long)model.year ,(long)model.mouth];
  66. self.selectionStyle = UITableViewCellSelectionStyleNone;
  67. DayModel *m = model.days.firstObject;
  68. if (m.dayOfTheWeek == 1) {
  69. _count = 6;
  70. }else{
  71. _count = m.dayOfTheWeek - 2;
  72. }
  73. [self.dataArray removeAllObjects];
  74. [self.dataArray addObjectsFromArray:model.days];
  75. [self.collectionView reloadData];
  76. }
  77. -(void)setToday:(BOOL )isToday
  78. {
  79. _isHaveToday = isToday;
  80. [self.collectionView reloadData];
  81. }
  82. - (void)awakeFromNib {
  83. [super awakeFromNib];
  84. _isHaveToday = NO;
  85. }
  86. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  87. {
  88. if ((self.dataArray.count + _count)%7 == 0) {
  89. return (self.dataArray.count + _count) / 7 * 7;
  90. }
  91. return ((self.dataArray.count + _count)/7 + 1)*7;
  92. }
  93. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. DayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DayCell" forIndexPath:indexPath];
  96. if (indexPath.row < _count || indexPath.row >= self.dataArray.count + _count) {
  97. [cell fullCellWithModel:nil];
  98. }else{
  99. DayModel *m = self.dataArray[indexPath.row - _count];
  100. [cell fullCellWithModel:m];
  101. }
  102. if (_isHaveToday && indexPath.row == _count) {
  103. [cell setTodayText:YES];
  104. }else{
  105. [cell setTodayText:NO];
  106. }
  107. return cell;
  108. }
  109. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  110. {
  111. return UIEdgeInsetsMake(2, 2, 2, 2);
  112. }
  113. -(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  114. {
  115. return 2;
  116. }
  117. -(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  118. {
  119. return 2;
  120. }
  121. -(CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. return CGSizeMake(([UIScreen mainScreen].bounds.size.width - 16)/7, 60);
  124. }
  125. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. if (indexPath.row < _count || indexPath.row >= self.dataArray.count + _count) {
  128. return ;
  129. }else{
  130. DayModel *m = self.dataArray[indexPath.row - _count];
  131. if (self.selectedDay) {
  132. self.selectedDay(m.day);
  133. }
  134. }
  135. }
  136. -(void)selectedDay:(SelectedDay)selectedDay
  137. {
  138. _selectedDay = selectedDay;
  139. }
  140. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  141. [super setSelected:selected animated:animated];
  142. // Configure the view for the selected state
  143. }
  144. @end