// // HDLPickerView.m // HDL_Widget_iOS // // Created by HDL on 2019/9/25. // Copyright © 2019 JLChen. All rights reserved. // #import "HDLPickerView.h" @interface HDLPickerView () @property (nonatomic, strong) UIPickerView *pickerView; //2019-9-5 新增默认选择项,和回调选中的索引 @property (nonatomic, assign) NSInteger selectIndex1; @property (nonatomic, assign) NSInteger selectIndex2; @property (nonatomic, assign) NSInteger selectIndex3; //@property (nonatomic, strong) UIColor *btnColor; @property (nonatomic, assign) BOOL isLinked; //是否不联动 @property (nonatomic, assign) NSUInteger component; // component numbers, default 0 @property (nonatomic, assign) CGFloat pickerViewHeight; // pickerView height, default 224 pt @property (nonatomic, assign) CGFloat oneComponentRowHeight; // one component row height, default 32 pt // property @property (nonatomic, strong) NSMutableArray *dataList; // data list @property (nonatomic, strong) NSMutableArray *mSecondDataList; // 二级 list @property (nonatomic, strong) NSMutableArray *mThirdDataList; // 三级 list //@property (nonatomic, strong) NSDictionary *selectRowTitleAttribute; // select row titlt attribute @property (nonatomic, strong) NSDictionary *selectRowTitleAttribute0; // select row titlt attribute @property (nonatomic, strong) NSDictionary *selectRowTitleAttribute1; // select row titlt attribute @property (nonatomic, strong) NSDictionary *selectRowTitleAttribute2; // select row titlt attribute @end @implementation HDLPickerView /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // self.selectRowTitleAttribute = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute0 = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute1 = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute2 = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.pickerViewHeight = 224.0f; self.oneComponentRowHeight = 32.0f; _dataList = [NSMutableArray array]; _mSecondDataList = [NSMutableArray array]; _mThirdDataList = [NSMutableArray array]; // self.isLinked = NO; [self addSubview:self.pickerView]; self.backgroundColor = [UIColor whiteColor]; } return self; } #pragma mark- 懒加载 - (UIPickerView *)pickerView{ if (_pickerView == nil) { _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; _pickerView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); _pickerView.dataSource = self; _pickerView.delegate = self; } return _pickerView; } #pragma mark - UIPickerView DataSource & Delegate - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return self.component; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(self.isLinked){ return [self getDataWithComponent:component].count; // return 0; }else{ return [self getDataWithComponentNoLinked:component].count; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSArray *componentArray; if(self.isLinked){ componentArray = [self getDataWithComponent:component]; }else{ componentArray = [self getDataWithComponentNoLinked:component]; } if (componentArray.count) { if (row < componentArray.count) { id titleData = componentArray[row]; if ([titleData isKindOfClass:[NSString class]]) { return titleData; } else if ([titleData isKindOfClass:[NSNumber class]]) { return [NSString stringWithFormat:@"%@", titleData]; } } } return @""; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if(self.isLinked){ if(component == 0){ self.selectIndex1 = row; self.selectIndex2 = 0; }else if(component == 1){ self.selectIndex2 = row; self.selectIndex3 = 0; }else if(component == 2){ self.selectIndex3 = row; } [pickerView reloadAllComponents]; for (NSUInteger i = 0; i < self.component; i++) { if (i > component) { [pickerView selectRow:0 inComponent:i animated:YES]; } } }else{ // [pickerView selectRow:row inComponent:component animated:YES]; if(component == 0){ self.selectIndex1 = row; }else if(component == 1){ self.selectIndex2 = row; }else if(component == 2){ self.selectIndex3 = row; } } if(self.mChangedDelegate != nil){ [self.mChangedDelegate onOptionsSelectChangeListener:_selectIndex1 selectIndex2:_selectIndex2 selectIndex3:_selectIndex3]; } } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return self.oneComponentRowHeight; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { // set separateline color // if (NO == self.isSettedSelectRowLineBackgroundColor) { // UIView *topSeparateLine = [pickerView.subviews objectAtIndex:1]; // UIView *bottomSeparateLine = [pickerView.subviews objectAtIndex:2]; // if (topSeparateLine.frame.size.height < 1.0f && // bottomSeparateLine.frame.size.height < 1.0f) { // topSeparateLine.backgroundColor = self.selectRowLineBackgroundColor; // bottomSeparateLine.backgroundColor = self.selectRowLineBackgroundColor; // self.isSettedSelectRowLineBackgroundColor = YES; // } else { // for (UIView *singleLine in pickerView.subviews) { // if (singleLine.frame.size.height < 1.0f) { // singleLine.backgroundColor = self.selectRowLineBackgroundColor; // self.isSettedSelectRowLineBackgroundColor = YES; // } // } // } // } // custom pickerView content label UILabel *pickerLabel = (UILabel *)view; // discussion: this is always nil, not Reusing, it's an iOS System bug. // reference: https://stackoverflow.com/questions/20635949/reusing-view-in-uipickerview-with-ios-7/21039321#21039321 if (!pickerLabel) { pickerLabel = [[UILabel alloc] init]; pickerLabel.textAlignment = NSTextAlignmentCenter; pickerLabel.adjustsFontSizeToFitWidth = YES; pickerLabel.backgroundColor = [UIColor clearColor]; } pickerLabel.attributedText = [self pickerView:pickerView attributedTitleForRow:row forComponent:component]; return pickerLabel; } // - (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString *normalRowString = [self pickerView:pickerView titleForRow:row forComponent:component]; if(component == 1){ return [[NSAttributedString alloc] initWithString:normalRowString attributes:self.selectRowTitleAttribute1]; }else if(component == 2){ return [[NSAttributedString alloc] initWithString:normalRowString attributes:self.selectRowTitleAttribute2]; }else{ return [[NSAttributedString alloc] initWithString:normalRowString attributes:self.selectRowTitleAttribute0]; } } - (NSArray *)getDataWithComponentNoLinked:(NSInteger)component { // NSMutableArray *arrayM = [NSMutableArray array]; if(component == 0){ return self.dataList; }else if(component == 1){ return self.mSecondDataList; }else if(component == 2){ return self.mThirdDataList; }else{ NSMutableArray *arrayM = [NSMutableArray array]; return arrayM; } } - (NSArray *)getDataWithComponent:(NSInteger)component { NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSInteger i = 0; i <= component; i++) { if (i == component) { id data = tempArray.firstObject; if ([data isKindOfClass:[NSDictionary class]]) { NSMutableArray *tempTitleArray = [NSMutableArray arrayWithArray:tempArray]; [tempArray removeAllObjects]; [tempTitleArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) { [tempArray addObjectsFromArray:dict.allKeys]; }]; [arrayM addObjectsFromArray:tempArray]; } else if ([data isKindOfClass:[NSString class]] || [data isKindOfClass:[NSNumber class]]){ [arrayM addObjectsFromArray:tempArray]; } } else { NSInteger selectRow = [self.pickerView selectedRowInComponent:i]; if (selectRow < tempArray.count) { id data = tempArray[selectRow]; if ([data isKindOfClass:[NSDictionary class]]) { [tempArray removeAllObjects]; NSDictionary *dict = data; [dict.allValues enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[NSArray class]]) { [tempArray addObjectsFromArray:obj]; } else { [tempArray addObject:obj]; } }]; } } } } return arrayM; } -(void)setCurrentItems:(NSInteger)selectIndex1 selectIndex2:(NSInteger)selectIndex2 selectIndex3:(NSInteger)selectIndex3{ self.selectIndex1 = selectIndex1; self.selectIndex2 = selectIndex2; self.selectIndex3 = selectIndex3; [self.pickerView reloadAllComponents]; [self scrollToSelectedRow]; } /* * 2019-09-05 根据索引选中,替换原来根据传入字符选中方案 */ - (void)scrollToSelectedRow { for (NSUInteger i = 0; i < self.component; i++) { if(i == 0){ [self.pickerView selectRow:self.selectIndex1 inComponent:i animated:NO]; }else if(i == 1){ [self.pickerView selectRow:self.selectIndex2 inComponent:i animated:NO]; }else if(i == 2){ [self.pickerView selectRow:self.selectIndex3 inComponent:i animated:NO]; } } } /** 重置布局 @param mCGRect 布局 */ -(void)initWithFrameArc:(CGRect) mCGRect{ self.frame = mCGRect; _pickerView.frame = self.frame; _pickerView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); } /** 设置指定位置 分割线之间的文字的颜色 */ -(void)setTextColorCenter:(UIColor *)mColor optionId:(NSInteger)optionId{ if(optionId == 0){ self.selectRowTitleAttribute0 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; }else if(optionId == 1){ self.selectRowTitleAttribute1 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; }else if(optionId == 2){ self.selectRowTitleAttribute2 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; } } /** 设置指定位置 分割线之间的文字的颜色 */ -(void)setTextColorCenterAll:(UIColor *)mColor{ self.selectRowTitleAttribute0 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute1 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute2 = @{NSForegroundColorAttributeName : mColor, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; } /** 单独设置指定位置 分割线之间的文字的颜色 */ -(void)setTextColorCenterAlone:(UIColor *)mColor0 mColor1:(UIColor *)mColor1 mColor2:(UIColor *)mColor2 { self.selectRowTitleAttribute0 = @{NSForegroundColorAttributeName : mColor0, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute1 = @{NSForegroundColorAttributeName : mColor1, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; self.selectRowTitleAttribute2 = @{NSForegroundColorAttributeName : mColor2, NSFontAttributeName : [UIFont systemFontOfSize:20.0f]}; } - (void)setNPicker:(nonnull NSArray *)dataList mSecondList:(nullable NSArray *)mSecondList mThirdList:(nullable NSArray *)mThirdList{ self.isLinked = NO; //配置关键数据 if (dataList && dataList.count > 0) { self.component = 1; [_dataList removeAllObjects]; [_dataList addObjectsFromArray:dataList]; } if (mSecondList && mSecondList.count > 0) { self.component = 2; [_mSecondDataList removeAllObjects]; [_mSecondDataList addObjectsFromArray:mSecondList]; } if (mThirdList && mThirdList.count > 0) { self.component = 3; [_mThirdDataList removeAllObjects]; [_mThirdDataList addObjectsFromArray:mThirdList]; } } -(void)setPicker:(nonnull NSArray *)dataList{ // no data if (!dataList || dataList.count == 0) { return; } [_dataList removeAllObjects]; [_dataList addObjectsFromArray:dataList]; // calculate component num id data = dataList.firstObject; if ([data isKindOfClass:[NSString class]] || [data isKindOfClass:[NSNumber class]] ) { self.component = 1; } else if ([data isKindOfClass:[NSDictionary class]]) { self.component ++; [self handleDictDataList:dataList]; } else { NSLog(@"ZJPickerView error tip:\"Unsupported data type\""); return; } self.isLinked = YES; } #pragma mark - private method - (void)handleDictDataList:(NSArray *)list{ id data = list.firstObject; if ([data isKindOfClass:[NSDictionary class]]) { NSDictionary *dict = data; id value = dict.allValues.firstObject; if ([value isKindOfClass:[NSArray class]]) { self.component++; [self handleDictDataList:value]; } } } @end