// // SPSBChooseCityTableViewController.m // 我的社保 // // Created by shanp on 2021/5/20. // #import "SPSBChooseCityTableViewController.h" #import "SPSBUIGeneralHeader.h" #define spsb_firstCellButtonTag 4000 #define spsb_cellLabelTag 3000 @interface SPSBChooseCityTableViewController () { UITableViewCell *_firstSectionCell; CGFloat _firstSectionHeigh; NSDictionary *_data; } @end @implementation SPSBChooseCityTableViewController - (instancetype)initWithData:(NSDictionary *)data { self = [super initWithStyle:UITableViewStylePlain backgroundColor:SPSBTableViewBackgroundColorGray]; if (!self) return nil; _data = data; _firstSectionCell = [self creatFirstSection]; return self; } - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - Action - (void)selectHotCity:(UIButton *)sender { NSString *cityId = _data[@"id"][0][@"热"][sender.tag - spsb_firstCellButtonTag]; _spsb_choose(cityId); } #pragma mark - Network Action #pragma mark - Delegate & DataSource static NSString * const reuseIdentifier = @"SPSBChooseCityTableViewCell"; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return _firstSectionCell; } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; UILabel *label = [UILabel convenienceWithFont:spsb_font(16) text:nil textColor:spsb_333333_color()]; label.tag = spsb_cellLabelTag; [cell.contentView addSubview:label]; [label makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(15); make.centerY.equalTo(0); }]; //线 [cell.contentView createLineWithLocation:JXHLineLocationBottom headOffset:15 footOffset:0]; } UILabel *label = (UILabel *)[cell viewWithTag:spsb_cellLabelTag]; NSDictionary *dict = _data[@"data"][indexPath.section]; NSString *key = _data[@"key"][indexPath.section]; label.text = dict[key][indexPath.row]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ((NSArray *)_data[@"key"]).count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) return 1; NSDictionary *dict = _data[@"data"][section]; NSString *key = _data[@"key"][section]; return ((NSArray *)dict[key]).count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) return _firstSectionHeigh; return 44; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:false]; NSString *cityId = _data[@"id"][indexPath.section][_data[@"key"][indexPath.section]][indexPath.row]; _spsb_choose(cityId); } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } static NSString * const sectionIdentifier = @"SPSBChooseCityTableViewHeader"; - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UITableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionIdentifier]; if (!sectionView) { sectionView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:sectionIdentifier]; sectionView.contentView.backgroundColor = spsb_F5F5F5_color(); UILabel *sectionLabel = [UILabel convenienceWithFont:spsb_font(14) text:nil textColor:spsb_666666_color()]; sectionLabel.tag = 3000; [sectionView.contentView addSubview:sectionLabel]; [sectionLabel makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(15); make.bottom.equalTo(-5); }]; } UILabel *label = [sectionView.contentView viewWithTag:3000]; label.text = _data[@"key"][section]; return sectionView; } #pragma mark - UI - (UITableViewCell *)creatFirstSection { UITableViewCell *firstSectionCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; firstSectionCell.contentView.backgroundColor = spsb_F5F5F5_color(); firstSectionCell.selectionStyle = UITableViewCellSelectionStyleNone; UIButton *tempHorizontalButton = nil; UIButton *tempVerticalButton = nil; NSArray *dataArray = _data[@"data"][0][@"热"]; CGFloat rowNumber = dataArray.count % 3 == 0 ? dataArray.count / 3 : dataArray.count / 3 + 1; _firstSectionHeigh = 0.f; for (NSInteger i = 0; i < dataArray.count; i ++) { UIButton *button = [UIButton convenienceWithFont:spsb_font(16) target:self action:@selector(selectHotCity:)]; [button setTitle:dataArray[i] titleColor:spsb_333333_color() state:JXHButtonControlStateNormal]; button.tag = spsb_firstCellButtonTag + i; button.backgroundColor = spsb_FFFFFF_color(1.f); [firstSectionCell.contentView addSubview:button]; [button makeConstraints:^(JXHConstraintMaker *make) { if (tempVerticalButton) { make.top.equalTo(tempVerticalButton.bottom).offset(10).priority(1000); make.width.equalTo(tempVerticalButton.width).priority(1000); } else { make.top.equalTo(0).priority(1000); } make.height.equalTo(40).priority(1000); if (tempHorizontalButton) { make.leading.equalTo(tempHorizontalButton.trailing).offset(10).priority(1000); make.width.equalTo(tempHorizontalButton.width).priority(1000); } else { self->_firstSectionHeigh += 50.f; make.leading.equalTo(15).priority(1000); } if ((i + 1) % 3 == 0) { make.trailing.equalTo(-15).priority(1000); } if (i / 3 == rowNumber - 1) { make.bottom.equalTo(-10).priority(1000); } }]; if ((i + 1) % 3 == 0) { tempVerticalButton = button; tempHorizontalButton = nil; } else { tempHorizontalButton = button; } } return firstSectionCell; } @end #undef spsb_firstCellButtonTag #undef spsb_cellLabelTag