wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
//  HDLPickerView.m
//  HDL_Widget_iOS
//
//  Created by HDL on 2019/9/25.
//  Copyright © 2019 JLChen. All rights reserved.
//
 
#import "HDLPickerView.h"
@interface HDLPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
 
@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