KWMineMoreProductsCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // KWMineMoreProductsCell.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2022/9/15.
  6. //
  7. #import "KWMineMoreProductsCell.h"
  8. #import "KWMineMoreProductTypeCell.h"
  9. #import "HomeFlashDealSubCollectCell.h"
  10. @interface KWMineMoreProductsCell ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
  11. @property (nonatomic, strong) HomeFilterModel *currentKey;
  12. @property (nonatomic, strong) NSArray<NSNumber *> *keyWidthArr;
  13. @end
  14. @implementation KWMineMoreProductsCell
  15. - (void)setTopSpace:(CGFloat)topSpace {
  16. _topSpace = topSpace;
  17. [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
  18. make.top.equalTo(self.contentView).offset(topSpace);
  19. make.centerX.equalTo(self.contentView);
  20. make.left.greaterThanOrEqualTo(self.contentView).offset(20);
  21. make.height.equalTo(@34);
  22. make.width.greaterThanOrEqualTo(@80);
  23. }];
  24. }
  25. -(void)setData:(KWMineMoreProductModel *)model {
  26. self.model = model;
  27. for (HomeFilterModel *type in self.model.keysArr) {
  28. if (type.selected) {
  29. self.currentKey = type;
  30. }
  31. }
  32. if (!self.currentKey) {
  33. self.currentKey = self.model.keysArr.firstObject;
  34. }
  35. self.titleLb.text = self.model.title;
  36. [self calulateItemWidth];
  37. [self.typeCollectV reloadData];
  38. [self.collectV reloadData];
  39. }
  40. -(void)calulateItemWidth {
  41. NSMutableArray<NSNumber *> *tempArr = [NSMutableArray array];
  42. NSDictionary *attribute = @{NSFontAttributeName: self.titleLb.font};
  43. for (HomeFilterModel *typeM in self.model.keysArr) {
  44. NSString *key = typeM.title;
  45. CGFloat wid = [key boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 34) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size.width + 10;
  46. // CGFloat wid = [key widthForFont:[UIFont fontWithName:Rob_Regular size:12]] + 10;
  47. [tempArr addObject:[NSNumber numberWithFloat:wid]];
  48. }
  49. int numLins = tempArr.count > 0 ? 1 : 0;
  50. CGFloat totalWid = 0;
  51. CGFloat maxWid = KScreenWidth - 20;
  52. int lineBeginI = 0;
  53. for (int i = 0; i < tempArr.count; i++) {
  54. CGFloat tempW = tempArr[i].floatValue;
  55. if ((tempW + totalWid + (i-lineBeginI)*10) > maxWid) {
  56. if (i>lineBeginI) {
  57. int num = i-lineBeginI;
  58. CGFloat elseSpace = (maxWid - totalWid - (num-1)*10)/num;
  59. for (int j = lineBeginI; j<i; j++) {
  60. CGFloat tempSubW = tempArr[j].floatValue;
  61. tempArr[j] = [NSNumber numberWithFloat: tempSubW + elseSpace];
  62. }
  63. }
  64. if (i == tempArr.count - 1) {
  65. tempArr[i] = [NSNumber numberWithFloat: maxWid];
  66. break;
  67. }
  68. totalWid = tempW;
  69. lineBeginI = i;
  70. numLins += 1;
  71. } else {
  72. totalWid += tempW;
  73. if (i == tempArr.count - 1) {
  74. if (tempArr.count>lineBeginI) {
  75. int num = (int)(tempArr.count) - lineBeginI;
  76. CGFloat elseSpace = (maxWid - totalWid- (num-1)*10)/num;
  77. for (int j = lineBeginI; j<tempArr.count; j++) {
  78. CGFloat tempSubW = tempArr[j].floatValue;
  79. tempArr[j] = [NSNumber numberWithFloat: tempSubW + elseSpace];
  80. }
  81. }
  82. }
  83. }
  84. }
  85. self.keyWidthArr = tempArr;
  86. @weakify(self);
  87. [self.typeCollectV mas_remakeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(weak_self.titleLb.mas_bottom).offset(20);
  89. make.height.equalTo([NSNumber numberWithInteger:20 + numLins * 40 + (numLins-1)*10]);
  90. make.left.right.equalTo(weak_self.contentView);
  91. }];
  92. }
  93. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  94. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  95. if (self) {
  96. [self loadSubV];
  97. }
  98. return self;
  99. }
  100. - (void)loadSubV {
  101. self.selectionStyle = UITableViewCellSelectionStyleNone;
  102. self.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
  103. self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
  104. [self.contentView addSubview:self.titleLb];
  105. [self.contentView addSubview:self.typeCollectV];
  106. [self.contentView addSubview:self.collectV];
  107. [self.contentView addSubview:self.offsetV];
  108. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.top.equalTo(self.contentView).offset(20);
  110. make.centerX.equalTo(self.contentView);
  111. make.left.greaterThanOrEqualTo(self.contentView).offset(20);
  112. make.height.equalTo(@34);
  113. make.width.greaterThanOrEqualTo(@80);
  114. }];
  115. [self.typeCollectV mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.top.equalTo(self.titleLb.mas_bottom).offset(20);
  117. make.height.equalTo(@110);
  118. make.left.right.equalTo(self.contentView);
  119. }];
  120. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  121. [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.left.equalTo(self.contentView).offset(10);
  123. make.right.equalTo(self.contentView).offset(-10);
  124. make.height.equalTo([NSNumber numberWithFloat:collH]);
  125. make.top.equalTo(self.typeCollectV.mas_bottom).offset(10);
  126. }];
  127. [self.offsetV mas_makeConstraints:^(MASConstraintMaker *make) {
  128. make.centerX.equalTo(self.contentView);
  129. make.height.equalTo(@2);
  130. make.width.equalTo(@137);
  131. make.top.equalTo(self.collectV.mas_bottom).offset(20);
  132. make.bottom.equalTo(self.contentView).offset(-40);
  133. }];
  134. }
  135. - (UILabel *)titleLb {
  136. if (!_titleLb) {
  137. UILabel *lb = [[UILabel alloc] init];
  138. lb.textColor = [UIColor blackColor];
  139. lb.font = [UIFont fontWithName:Rob_Bold size:24];
  140. lb.textAlignment = NSTextAlignmentCenter;
  141. _titleLb = lb;
  142. }
  143. return _titleLb;
  144. }
  145. - (KWScrollOffsetView *)offsetV {
  146. if (!_offsetV) {
  147. KWScrollOffsetView *v = [[KWScrollOffsetView alloc] initWithFrame:CGRectMake(0, 0, 137, 2)];
  148. v.clipsToBounds = true;
  149. v.offWidth = 54;
  150. v.offset = 0;
  151. _offsetV = v;
  152. }
  153. return _offsetV;
  154. }
  155. -(UICollectionView *)collectV {
  156. if (!_collectV) {
  157. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  158. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  159. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  160. CGFloat collH = (KScreenWidth-30)/2 + 126;
  161. layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
  162. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  163. collV.backgroundColor = [UIColor whiteColor];
  164. collV.alwaysBounceHorizontal = true;
  165. collV.delegate = self;
  166. collV.dataSource = self;
  167. collV.showsHorizontalScrollIndicator = false;
  168. [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
  169. _collectV = collV;
  170. }
  171. return _collectV;
  172. }
  173. -(UICollectionView *)typeCollectV {
  174. if (!_typeCollectV) {
  175. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  176. layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
  177. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  178. layout.itemSize = CGSizeMake(100, 40);
  179. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 110) collectionViewLayout:layout];
  180. collV.scrollEnabled = false;
  181. collV.backgroundColor = [UIColor whiteColor];
  182. collV.alwaysBounceVertical = true;
  183. collV.delegate = self;
  184. collV.dataSource = self;
  185. collV.showsVerticalScrollIndicator = false;
  186. collV.showsHorizontalScrollIndicator = false;
  187. [collV registerClass:[KWMineMoreProductTypeCell class] forCellWithReuseIdentifier:@"KWMineMoreProductTypeCell"];
  188. _typeCollectV = collV;
  189. }
  190. return _typeCollectV;
  191. }
  192. - (void)awakeFromNib {
  193. [super awakeFromNib];
  194. // Initialization code
  195. }
  196. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  197. [super setSelected:selected animated:animated];
  198. // Configure the view for the selected state
  199. }
  200. // MARK: - UICollectionViewDelegate
  201. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  202. if (self.offsetV.isHidden) {
  203. return;
  204. }
  205. CGFloat offset = scrollView.contentOffset.x;
  206. CGFloat itemWidth = (KScreenWidth-30)/2;
  207. NSArray<ASProductBaseModel*> *arr = self.currentKey.list;
  208. if (arr.count > 0) {
  209. CGFloat maxOffset = (arr.count * (itemWidth + 10) - 10 ) - (KScreenWidth-20);
  210. self.offsetV.offset = offset/maxOffset;
  211. }
  212. }
  213. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  214. if (collectionView == self.typeCollectV) {
  215. for (int i = 0; i < self.model.keysArr.count; i++) {
  216. HomeFilterModel *item = self.model.keysArr[i];
  217. item.selected = i == indexPath.row;
  218. }
  219. HomeFilterModel *key = self.model.keysArr[indexPath.row];
  220. self.currentKey = key;
  221. [self.typeCollectV reloadData];
  222. [self.collectV reloadData];
  223. return;
  224. }
  225. if (self.productClick) {
  226. NSArray<ASProductBaseModel*> *arr = self.currentKey.list;
  227. ASProductBaseModel *m = arr[indexPath.row];
  228. self.productClick(indexPath.row, m);
  229. }
  230. }
  231. // MARK: - UICollectionViewDataSource
  232. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  233. if (collectionView == self.typeCollectV) {
  234. KWMineMoreProductTypeCell *ce = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWMineMoreProductTypeCell" forIndexPath:indexPath];
  235. HomeFilterModel *ti = self.model.keysArr[indexPath.row];
  236. [ce setTitle:ti.title isSelect:ti.selected];
  237. return ce;
  238. }
  239. HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath];
  240. NSArray<ASProductBaseModel*> *arr = self.currentKey.list;
  241. ASProductBaseModel *m = arr[indexPath.row];
  242. cell.model = m;
  243. return cell;
  244. }
  245. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  246. if (collectionView == self.typeCollectV) {
  247. return self.model.keysArr.count;
  248. }
  249. NSArray<ASProductBaseModel*> *arr = self.currentKey.list;
  250. return arr.count;
  251. }
  252. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  253. if (collectionView == self.typeCollectV) {
  254. CGFloat wid = self.keyWidthArr[indexPath.row].floatValue - 0.5;
  255. return CGSizeMake(wid, 40);
  256. }
  257. CGFloat marg = 10;
  258. CGFloat w = (KScreenWidth-3*marg)/2;
  259. CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
  260. return CGSizeMake(w, collH);
  261. }
  262. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  263. return 10;
  264. }
  265. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  266. return 10;
  267. }
  268. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  269. if (collectionView == self.typeCollectV) {
  270. return UIEdgeInsetsMake(10, 10, 10, 10);
  271. }
  272. return UIEdgeInsetsMake(20, 0, 10, 0);
  273. }
  274. @end