MonthTableViewCell.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // MonthTableViewCell.m
  3. // BJTResearch
  4. //
  5. // Created by yunlong on 17/5/12.
  6. // Copyright © 2017年 yunlong. All rights reserved.
  7. //
  8. #import "MonthTableViewCell.h"
  9. #import "MonthModel.h"
  10. #import "DayCollectionViewCell.h"
  11. #import "ADHTuanCan-Swift.h"
  12. #define KScreenWidth [UIScreen mainScreen].bounds.size.width
  13. #define itemWHSize = CGFloat(60.0)
  14. @interface MonthTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  15. @property(nonatomic,strong)UICollectionView *collectionView;
  16. @property(nonatomic,strong)UILabel *dateLabel;
  17. @end
  18. @implementation MonthTableViewCell
  19. + (instancetype)cellWithTableView:(UITableView *)tableView{
  20. static NSString *CellID = @"MonthTableViewCellID";
  21. MonthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
  22. if (cell==nil) {
  23. cell = [[MonthTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
  24. }
  25. cell.userInteractionEnabled = YES;
  26. return cell;
  27. }
  28. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  29. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  30. if (self) {
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. [self setContentView];
  33. }
  34. return self;
  35. }
  36. - (void)setContentView{
  37. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 9)];
  38. lineView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
  39. [self.contentView addSubview:lineView];
  40. _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 48 )];
  41. _dateLabel.textAlignment = NSTextAlignmentCenter;
  42. _dateLabel.font = [UIFont systemFontOfSize:20];
  43. [self.contentView addSubview:_dateLabel];
  44. [self.contentView addSubview:self.collectionView];
  45. }
  46. -(UICollectionView *)collectionView
  47. {
  48. if (!_collectionView) {
  49. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  50. flowLayout.minimumLineSpacing = 0;
  51. flowLayout.minimumInteritemSpacing = 0;
  52. flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  53. flowLayout.itemSize = CGSizeMake(KScreenWidth/7, 60);
  54. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60, [UIScreen mainScreen].bounds.size.width, 500) collectionViewLayout:flowLayout];
  55. _collectionView.dataSource = self;
  56. _collectionView.delegate = self;
  57. _collectionView.bounces = NO;
  58. _collectionView.scrollEnabled = NO;
  59. _collectionView.backgroundColor = [UIColor whiteColor];
  60. [_collectionView registerClass:[DayCollectionViewCell class] forCellWithReuseIdentifier:@"DayCollectionViewCellID"];
  61. }
  62. return _collectionView;
  63. }
  64. #pragma mark - 设置内容
  65. - (void)setModel:(MonthModel *)model{
  66. _model = model;
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. CGRect frame = self.collectionView.frame;
  69. frame.size.height = model.cellHight - 60;
  70. self.collectionView.frame = frame;
  71. });
  72. NSString * yearStr = [NSString stringWithFormat:@"%04ld年%02ld月",model.year ,model.month];
  73. if ([LanguagesUtil isEnLanguage]){
  74. yearStr = [NSString stringWithFormat:@"%04ld-%02ld",model.year ,model.month];
  75. }
  76. self.dateLabel.text = yearStr;
  77. self.selectionStyle = UITableViewCellSelectionStyleNone;
  78. [self.collectionView reloadData];
  79. }
  80. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  81. return _model.cellNum;
  82. }
  83. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  84. DayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DayCollectionViewCellID" forIndexPath:indexPath];
  85. if ((indexPath.row < _model.cellStartNum) || (indexPath.row >= (_model.days.count + _model.cellStartNum))) {
  86. cell.model = nil;
  87. }else{
  88. DayModel *model = _model.days[indexPath.row - _model.cellStartNum];
  89. cell.model = model;
  90. }
  91. return cell;
  92. }
  93. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  94. if ((indexPath.row < _model.cellStartNum) || (indexPath.row >= (_model.days.count + _model.cellStartNum))) {
  95. return;
  96. }else{
  97. DayModel *model = _model.days[indexPath.row - _model.cellStartNum];
  98. if (self.selectedDay) {
  99. self.selectedDay(model);
  100. }
  101. }
  102. }
  103. @end