SPSBPickerView.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // SPSBPickerView.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/14.
  6. //
  7. #import "SPSBPickerView.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #define spsb_buttonTag 3000
  10. @interface SPSBPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource> {
  11. SPSBPickerViewArray *_dataArray;
  12. NSArray *_rowCountArray;
  13. NSMutableArray *_selectedArray;
  14. UIPickerView *_pickerView;
  15. }
  16. @end
  17. @implementation SPSBPickerView
  18. - (instancetype)initWithData:(SPSBPickerViewArray *)data {
  19. self = [super init];
  20. if (!self) return nil;
  21. _dataArray = [data copy];
  22. [self initData];
  23. [self addButtonView];
  24. [self addPickerView];
  25. return self;
  26. }
  27. - (void)initData {
  28. _selectedArray = NSMutableArray.new;
  29. NSMutableArray *rowCountArray = NSMutableArray.new;
  30. for (NSInteger i = 0; i < _dataArray.count; i ++) {
  31. [rowCountArray addObject:@([_dataArray[i] count])];
  32. [_selectedArray addObject:@0];
  33. }
  34. _spsb_choseArray = [_selectedArray copy];
  35. _rowCountArray = [rowCountArray copy];
  36. }
  37. #pragma mark - Action
  38. - (void)buttonAction:(UIButton *)sender {
  39. if (_spsb_clickButton) {
  40. _spsb_clickButton(sender.tag - spsb_buttonTag);
  41. }
  42. }
  43. - (void)chooseRow:(NSInteger)row inComponent:(NSInteger)component animated:(bool)animated {
  44. [_pickerView selectRow:row inComponent:component animated:animated];
  45. [self pickerView:_pickerView didSelectRow:row inComponent:component];
  46. }
  47. - (NSInteger)choseRowInComponent:(NSInteger)component {
  48. return [_pickerView selectedRowInComponent:component];
  49. }
  50. - (void)reloadViewWithData:(SPSBPickerViewArray *)data {
  51. _dataArray = [data copy];
  52. NSMutableArray *rowCountArray = [@[] mutableCopy];
  53. for (NSInteger i = 0; i < _dataArray.count; i ++) {
  54. [rowCountArray addObject:@([_dataArray[i] count])];
  55. }
  56. _rowCountArray = [rowCountArray copy];
  57. [_pickerView reloadAllComponents];
  58. for (NSInteger i = 0; i < _selectedArray.count; i ++) {
  59. _selectedArray[i] = @([_pickerView selectedRowInComponent:i]);
  60. }
  61. _spsb_choseArray = [_selectedArray copy];
  62. }
  63. #pragma mark - UIPickerViewDelegate & UIPickerViewDataSource
  64. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  65. _selectedArray[component] = @(row);
  66. _spsb_choseArray = [_selectedArray copy];
  67. [_spsb_pickerViewDelegate pickerView:self didChoseRow:row inComponent:component];
  68. }
  69. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  70. return _dataArray[component][row];
  71. }
  72. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  73. return [_rowCountArray[component] integerValue];
  74. }
  75. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  76. return _dataArray.count;
  77. }
  78. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
  79. return 56;
  80. }
  81. #pragma mark - UI
  82. #define spsb_buttonViewHeight 44
  83. - (void)addButtonView {
  84. UIView *buttonView = [[UIView alloc] init];
  85. buttonView.backgroundColor = spsb_FFFFFF_color(1.f);
  86. [self addSubview:buttonView];
  87. [buttonView makeConstraints:^(JXHConstraintMaker *make) {
  88. make.top.and.leading.and.trailing.equalTo(0);
  89. make.height.equalTo(spsb_buttonViewHeight);
  90. }];
  91. NSArray *buttonTitleArray = @[@"取消", @"确定"];
  92. for (NSInteger i = 0; i < 2; i ++) {
  93. UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(buttonAction:)];
  94. [button setTitle:buttonTitleArray[i] titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  95. button.tag = spsb_buttonTag + i;
  96. [buttonView addSubview:button];
  97. [button makeConstraints:^(JXHConstraintMaker *make) {
  98. make.top.and.bottom.equalTo(0);
  99. make.width.equalTo(60);
  100. if (i == 0) {
  101. make.leading.equalTo(0);
  102. } else {
  103. make.trailing.equalTo(0);
  104. }
  105. }];
  106. }
  107. [buttonView createLineWithLocation:JXHLineLocationBottom locationOffset:0 headOffset:0 footOffset:0];
  108. }
  109. - (void)addPickerView {
  110. _pickerView = [[UIPickerView alloc] init];
  111. _pickerView.delegate = self;
  112. _pickerView.dataSource = self;
  113. [self addSubview:_pickerView];
  114. [_pickerView makeConstraints:^(JXHConstraintMaker *make) {
  115. make.top.equalTo(spsb_buttonViewHeight);
  116. make.leading.equalTo(20);
  117. make.trailing.equalTo(-20);
  118. make.height.equalTo(224);
  119. make.bottom.equalTo(0);
  120. }];
  121. }
  122. @end
  123. #undef spsb_buttonViewHeight
  124. #undef spsb_buttonTag