SPSBChooseCityTableViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // SPSBChooseCityTableViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/20.
  6. //
  7. #import "SPSBChooseCityTableViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #define spsb_firstCellButtonTag 4000
  10. #define spsb_cellLabelTag 3000
  11. @interface SPSBChooseCityTableViewController () {
  12. UITableViewCell *_firstSectionCell;
  13. CGFloat _firstSectionHeigh;
  14. NSDictionary *_data;
  15. }
  16. @end
  17. @implementation SPSBChooseCityTableViewController
  18. - (instancetype)initWithData:(NSDictionary *)data {
  19. self = [super initWithStyle:UITableViewStylePlain backgroundColor:SPSBTableViewBackgroundColorGray];
  20. if (!self) return nil;
  21. _data = data;
  22. _firstSectionCell = [self creatFirstSection];
  23. return self;
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. }
  28. #pragma mark - Action
  29. - (void)selectHotCity:(UIButton *)sender {
  30. NSString *cityId = _data[@"id"][0][@"热"][sender.tag - spsb_firstCellButtonTag];
  31. _spsb_choose(cityId);
  32. }
  33. #pragma mark - Network Action
  34. #pragma mark - Delegate & DataSource
  35. static NSString * const reuseIdentifier = @"SPSBChooseCityTableViewCell";
  36. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  37. if (indexPath.section == 0) {
  38. return _firstSectionCell;
  39. }
  40. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  41. if (!cell) {
  42. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
  43. UILabel *label = [UILabel convenienceWithFont:spsb_font(16) text:nil textColor:spsb_333333_color()];
  44. label.tag = spsb_cellLabelTag;
  45. [cell.contentView addSubview:label];
  46. [label makeConstraints:^(JXHConstraintMaker *make) {
  47. make.leading.equalTo(15);
  48. make.centerY.equalTo(0);
  49. }];
  50. //线
  51. [cell.contentView createLineWithLocation:JXHLineLocationBottom headOffset:15 footOffset:0];
  52. }
  53. UILabel *label = (UILabel *)[cell viewWithTag:spsb_cellLabelTag];
  54. NSDictionary *dict = _data[@"data"][indexPath.section];
  55. NSString *key = _data[@"key"][indexPath.section];
  56. label.text = dict[key][indexPath.row];
  57. return cell;
  58. }
  59. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  60. return ((NSArray *)_data[@"key"]).count;
  61. }
  62. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  63. if (section == 0) return 1;
  64. NSDictionary *dict = _data[@"data"][section];
  65. NSString *key = _data[@"key"][section];
  66. return ((NSArray *)dict[key]).count;
  67. }
  68. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  69. if (indexPath.section == 0) return _firstSectionHeigh;
  70. return 44;
  71. }
  72. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  73. [tableView deselectRowAtIndexPath:indexPath animated:false];
  74. NSString *cityId = _data[@"id"][indexPath.section][_data[@"key"][indexPath.section]][indexPath.row];
  75. _spsb_choose(cityId);
  76. }
  77. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  78. return 40;
  79. }
  80. static NSString * const sectionIdentifier = @"SPSBChooseCityTableViewHeader";
  81. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  82. UITableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionIdentifier];
  83. if (!sectionView) {
  84. sectionView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:sectionIdentifier];
  85. sectionView.contentView.backgroundColor = spsb_F5F5F5_color();
  86. UILabel *sectionLabel = [UILabel convenienceWithFont:spsb_font(14) text:nil textColor:spsb_666666_color()];
  87. sectionLabel.tag = 3000;
  88. [sectionView.contentView addSubview:sectionLabel];
  89. [sectionLabel makeConstraints:^(JXHConstraintMaker *make) {
  90. make.leading.equalTo(15);
  91. make.bottom.equalTo(-5);
  92. }];
  93. }
  94. UILabel *label = [sectionView.contentView viewWithTag:3000];
  95. label.text = _data[@"key"][section];
  96. return sectionView;
  97. }
  98. #pragma mark - UI
  99. - (UITableViewCell *)creatFirstSection {
  100. UITableViewCell *firstSectionCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  101. firstSectionCell.contentView.backgroundColor = spsb_F5F5F5_color();
  102. firstSectionCell.selectionStyle = UITableViewCellSelectionStyleNone;
  103. UIButton *tempHorizontalButton = nil;
  104. UIButton *tempVerticalButton = nil;
  105. NSArray *dataArray = _data[@"data"][0][@"热"];
  106. CGFloat rowNumber = dataArray.count % 3 == 0 ? dataArray.count / 3 : dataArray.count / 3 + 1;
  107. _firstSectionHeigh = 0.f;
  108. for (NSInteger i = 0; i < dataArray.count; i ++) {
  109. UIButton *button = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(selectHotCity:)];
  110. [button setTitle:dataArray[i] titleColor:spsb_333333_color() state:JXHButtonControlStateNormal];
  111. button.tag = spsb_firstCellButtonTag + i;
  112. button.backgroundColor = spsb_FFFFFF_color(1.f);
  113. [firstSectionCell.contentView addSubview:button];
  114. [button makeConstraints:^(JXHConstraintMaker *make) {
  115. if (tempVerticalButton) {
  116. make.top.equalTo(tempVerticalButton.bottom).offset(10).priority(1000);
  117. make.width.equalTo(tempVerticalButton.width).priority(1000);
  118. } else {
  119. make.top.equalTo(0).priority(1000);
  120. }
  121. make.height.equalTo(40).priority(1000);
  122. if (tempHorizontalButton) {
  123. make.leading.equalTo(tempHorizontalButton.trailing).offset(10).priority(1000);
  124. make.width.equalTo(tempHorizontalButton.width).priority(1000);
  125. } else {
  126. self->_firstSectionHeigh += 50.f;
  127. make.leading.equalTo(15).priority(1000);
  128. }
  129. if ((i + 1) % 3 == 0) {
  130. make.trailing.equalTo(-15).priority(1000);
  131. }
  132. if (i / 3 == rowNumber - 1) {
  133. make.bottom.equalTo(-10).priority(1000);
  134. }
  135. }];
  136. if ((i + 1) % 3 == 0) {
  137. tempVerticalButton = button;
  138. tempHorizontalButton = nil;
  139. } else {
  140. tempHorizontalButton = button;
  141. }
  142. }
  143. return firstSectionCell;
  144. }
  145. @end
  146. #undef spsb_firstCellButtonTag
  147. #undef spsb_cellLabelTag