/UITableViewLInkageDemo/Class/View/CompareDetail/CompareCell.m

https://github.com/HawkEleven/UITableViewLinkageDemo · Objective C · 87 lines · 62 code · 18 blank · 7 comment · 11 complexity · 0afde29e7368c296a34a416a33cd21b4 MD5 · raw file

  1. //
  2. // CompareCell.m
  3. // UITableViewLInkageDemo
  4. //
  5. // Created by Eleven on 2017/7/3.
  6. // Copyright © 2016年 Hawk. All rights reserved.
  7. //
  8. #import "CompareCell.h"
  9. #import "CarModel.h"
  10. #import "Header.h"
  11. @interface CompareItem : UIView
  12. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  13. + (instancetype)creatView;
  14. @end
  15. @implementation CompareItem
  16. + (instancetype)creatView {
  17. return [[[NSBundle mainBundle] loadNibNamed:@"CompareItem" owner:nil options:nil] objectAtIndex:0];
  18. }
  19. @end
  20. @interface CompareCell ()
  21. @property (nonatomic, strong) NSMutableArray<CompareItem *> *compareItems;
  22. @end
  23. @implementation CompareCell
  24. - (void)awakeFromNib {
  25. [super awakeFromNib];
  26. }
  27. + (instancetype)cellWithTableView:(UITableView *)tableView {
  28. static NSString * ID = @"CompareCell";
  29. CompareCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  30. if (!cell) {
  31. cell = [[CompareCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  32. }
  33. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  34. return cell;
  35. }
  36. - (void)setDatas:(NSArray<CarModel *> *)datas withIndex:(NSIndexPath *)indexPath {
  37. if (!self.compareItems.count) {
  38. for (NSInteger i = 0; i < datas.count; i ++) {
  39. CompareItem *item = [CompareItem creatView];
  40. [self.contentView addSubview:item];
  41. [self.compareItems addObject:item];
  42. item.frame = CGRectMake(ITEM_WIDTH * i, 0, ITEM_WIDTH, 40);
  43. }
  44. } else {
  45. if (self.compareItems.count > datas.count) {
  46. for (NSInteger i = self.compareItems.count - 1; i > datas.count - 1; i --) {
  47. [(CompareItem *)self.compareItems[i] removeFromSuperview];
  48. [self.compareItems removeObject:self.compareItems[i]];
  49. }
  50. } else if (self.compareItems.count < datas.count) {
  51. for (NSInteger i = self.compareItems.count; i < datas.count; i ++) {
  52. CompareItem *item = [CompareItem creatView];
  53. [self.contentView addSubview:item];
  54. [self.compareItems addObject:item];
  55. item.frame = CGRectMake(ITEM_WIDTH * i, 0, ITEM_WIDTH, 40);
  56. }
  57. }
  58. }
  59. for (NSInteger i = 0; i < datas.count; i ++) {
  60. self.compareItems[i].titleLabel.text = datas[i].groupParamsViewModelList[indexPath.section].paramList[indexPath.row].paramValue;
  61. }
  62. }
  63. - (NSMutableArray<CompareItem *> *)compareItems {
  64. if (!_compareItems) {
  65. _compareItems = [NSMutableArray array];
  66. }
  67. return _compareItems;
  68. }
  69. @end