123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // SPSBPickerView.m
- // 我的社保
- //
- // Created by shanp on 2021/5/14.
- //
- #import "SPSBPickerView.h"
- #import "SPSBUIGeneralHeader.h"
- #define spsb_buttonTag 3000
- @interface SPSBPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource> {
- SPSBPickerViewArray *_dataArray;
- NSArray *_rowCountArray;
- NSMutableArray *_selectedArray;
- UIPickerView *_pickerView;
- }
- @end
- @implementation SPSBPickerView
- - (instancetype)initWithData:(SPSBPickerViewArray *)data {
- self = [super init];
- if (!self) return nil;
- _dataArray = [data copy];
- [self initData];
- [self addButtonView];
- [self addPickerView];
- return self;
- }
- - (void)initData {
- _selectedArray = NSMutableArray.new;
- NSMutableArray *rowCountArray = NSMutableArray.new;
- for (NSInteger i = 0; i < _dataArray.count; i ++) {
- [rowCountArray addObject:@([_dataArray[i] count])];
- [_selectedArray addObject:@0];
- }
- _spsb_choseArray = [_selectedArray copy];
- _rowCountArray = [rowCountArray copy];
- }
- #pragma mark - Action
- - (void)buttonAction:(UIButton *)sender {
- if (_spsb_clickButton) {
- _spsb_clickButton(sender.tag - spsb_buttonTag);
- }
- }
- - (void)chooseRow:(NSInteger)row inComponent:(NSInteger)component animated:(bool)animated {
- [_pickerView selectRow:row inComponent:component animated:animated];
- [self pickerView:_pickerView didSelectRow:row inComponent:component];
- }
- - (NSInteger)choseRowInComponent:(NSInteger)component {
- return [_pickerView selectedRowInComponent:component];
- }
- - (void)reloadViewWithData:(SPSBPickerViewArray *)data {
- _dataArray = [data copy];
- NSMutableArray *rowCountArray = [@[] mutableCopy];
- for (NSInteger i = 0; i < _dataArray.count; i ++) {
- [rowCountArray addObject:@([_dataArray[i] count])];
- }
- _rowCountArray = [rowCountArray copy];
- [_pickerView reloadAllComponents];
- for (NSInteger i = 0; i < _selectedArray.count; i ++) {
- _selectedArray[i] = @([_pickerView selectedRowInComponent:i]);
- }
- _spsb_choseArray = [_selectedArray copy];
- }
- #pragma mark - UIPickerViewDelegate & UIPickerViewDataSource
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- _selectedArray[component] = @(row);
- _spsb_choseArray = [_selectedArray copy];
- [_spsb_pickerViewDelegate pickerView:self didChoseRow:row inComponent:component];
- }
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
- return _dataArray[component][row];
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
- return [_rowCountArray[component] integerValue];
- }
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
- return _dataArray.count;
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
- return 56;
- }
- #pragma mark - UI
- #define spsb_buttonViewHeight 44
- - (void)addButtonView {
- UIView *buttonView = [[UIView alloc] init];
- buttonView.backgroundColor = spsb_FFFFFF_color(1.f);
- [self addSubview:buttonView];
- [buttonView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.and.leading.and.trailing.equalTo(0);
- make.height.equalTo(spsb_buttonViewHeight);
- }];
-
- NSArray *buttonTitleArray = @[@"取消", @"确定"];
- for (NSInteger i = 0; i < 2; i ++) {
- UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(buttonAction:)];
- [button setTitle:buttonTitleArray[i] titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
- button.tag = spsb_buttonTag + i;
- [buttonView addSubview:button];
- [button makeConstraints:^(JXHConstraintMaker *make) {
- make.top.and.bottom.equalTo(0);
- make.width.equalTo(60);
- if (i == 0) {
- make.leading.equalTo(0);
- } else {
- make.trailing.equalTo(0);
- }
- }];
- }
-
- [buttonView createLineWithLocation:JXHLineLocationBottom locationOffset:0 headOffset:0 footOffset:0];
- }
- - (void)addPickerView {
- _pickerView = [[UIPickerView alloc] init];
- _pickerView.delegate = self;
- _pickerView.dataSource = self;
- [self addSubview:_pickerView];
- [_pickerView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(spsb_buttonViewHeight);
- make.leading.equalTo(20);
- make.trailing.equalTo(-20);
- make.height.equalTo(224);
- make.bottom.equalTo(0);
- }];
- }
- @end
- #undef spsb_buttonViewHeight
- #undef spsb_buttonTag
|