HomeGoodBookCell.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // HomeGoodBookCell.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/5/20.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "HomeGoodBookCell.h"
  9. #import "BookHomeSubCell.h"
  10. #import "HomeGoodBookCollectCell.h"
  11. @implementation HomeGoodBookCell
  12. - (void)awakeFromNib {
  13. [super awakeFromNib];
  14. self.collectionView.delegate = self;
  15. self.collectionView.dataSource = self;
  16. self.collectionView.showsVerticalScrollIndicator = NO;
  17. self.collectionView.showsHorizontalScrollIndicator = NO;
  18. [self.collectionView registerNib:[UINib nibWithNibName:@"BookHomeSubCell" bundle:nil] forCellWithReuseIdentifier:@"BookHomeSubCell"];
  19. [self.collectionView registerNib:[UINib nibWithNibName:@"HomeGoodBookCollectCell" bundle:nil] forCellWithReuseIdentifier:@"HomeGoodBookCollectCell"];
  20. }
  21. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  22. [super setSelected:selected animated:animated];
  23. // Configure the view for the selected state
  24. }
  25. + (HomeGoodBookCell *)configCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
  26. static NSString *cellIdentifer = @"HomeGoodBookCell";
  27. HomeGoodBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
  28. if (cell == nil) {
  29. cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeGoodBookCell" owner:nil options:nil] objectAtIndex:0];
  30. }
  31. return cell;
  32. }
  33. - (void)setDataWithItem:(NSInteger)Item isUnUser:(BOOL)isUser model:(HomeSubModel *)model
  34. {
  35. self.TitleL.text = model.LabelName;
  36. self.model = model;
  37. self.type = Item % 2 == 0 ? 0 : 1;
  38. if (self.type == 0) {
  39. self.collectH.constant = 101.f * model.Items.count;
  40. UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
  41. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  42. [self.collectionView setCollectionViewLayout:layout];
  43. }else{
  44. CGFloat H = 0.f;
  45. if (model.Items.count > 3) {
  46. if (model.Items.count % 3 == 0) {
  47. H = model.Items.count / 3 * 170.f;
  48. }else{
  49. H = model.Items.count / 3 * 170.f + 170.f;
  50. }
  51. }else{
  52. H = 170.f;
  53. }
  54. self.collectH.constant = H;
  55. UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
  56. layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
  57. [self.collectionView setCollectionViewLayout:layout];
  58. }
  59. [self.collectionView setContentOffset:CGPointZero animated:NO];
  60. [self.collectionView reloadData];
  61. WS(weakSelf);
  62. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  63. [weakSelf.collectionView reloadData];
  64. });
  65. }
  66. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  67. {
  68. return 0.f;
  69. }
  70. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  71. {
  72. return self.type == 0 ? 0.f : 15.f;
  73. }
  74. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  75. {
  76. return self.model.Items.count;
  77. }
  78. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. if (self.type == 0) {
  81. return CGSizeMake(SCREEN_WIDTH, 101);
  82. }else{
  83. return CGSizeMake((SCREEN_WIDTH - 60) / 3, 170);
  84. }
  85. }
  86. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. NSDictionary * dict = self.model.Items[indexPath.item];
  89. HomeSubItemModel * smodel = [HomeSubItemModel modelWithDictionary:dict];
  90. if (self.type == 0) {
  91. BookHomeSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BookHomeSubCell" forIndexPath:indexPath];
  92. [cell setData:smodel];
  93. return cell;
  94. }else{
  95. HomeGoodBookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodBookCollectCell" forIndexPath:indexPath];
  96. [cell setData:smodel];
  97. return cell;
  98. }
  99. }
  100. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  101. {
  102. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  103. NSDictionary * dict = self.model.Items[indexPath.item];
  104. HomeSubItemModel * model = [HomeSubItemModel modelWithDictionary:dict];
  105. if (self.ClickItemBlock) {
  106. self.ClickItemBlock(model);
  107. }
  108. }
  109. @end