BzwPicker.m 33.6 KB
Newer Older
xwenliang's avatar
xwenliang committed
1 2 3 4 5 6 7 8 9
//
//  BzwPicker.m
//  PickerView
//
//  Created by Bao on 15/12/14.
//  Copyright © 2015年 Microlink. All rights reserved.
//

#import "BzwPicker.h"
xwenliang's avatar
xwenliang committed
10
#define linSpace 5
xwenliang's avatar
xwenliang committed
11 12 13

@implementation BzwPicker

14
-(instancetype)initWithFrame:(CGRect)frame dic:(NSDictionary *)dic leftStr:(NSString *)leftStr centerStr:(NSString *)centerStr rightStr:(NSString *)rightStr topbgColor:(NSArray *)topbgColor bottombgColor:(NSArray *)bottombgColor leftbtnbgColor:(NSArray *)leftbtnbgColor rightbtnbgColor:(NSArray *)rightbtnbgColor centerbtnColor:(NSArray *)centerbtnColor selectValueArry:(NSArray *)selectValueArry  weightArry:(NSArray *)weightArry
15
       pickerToolBarFontSize:(NSString *)pickerToolBarFontSize  pickerFontSize:(NSString *)pickerFontSize  pickerFontColor:(NSArray *)pickerFontColor pickerRowHeight:(NSString *)pickerRowHeight pickerFontFamily:(NSString *)pickerFontFamily
xwenliang's avatar
xwenliang committed
16

xwenliang's avatar
xwenliang committed
17 18 19 20 21
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backArry=[[NSMutableArray alloc]init];
xwenliang's avatar
xwenliang committed
22 23
        self.provinceArray=[[NSMutableArray alloc]init];
        self.cityArray=[[NSMutableArray alloc]init];
xwenliang's avatar
xwenliang committed
24
        self.selectValueArry=selectValueArry;
25
        self.weightArry=weightArry;
xwenliang's avatar
xwenliang committed
26 27 28 29
        self.pickerDic=dic;
        self.leftStr=leftStr;
        self.rightStr=rightStr;
        self.centStr=centerStr;
xwenliang's avatar
xwenliang committed
30 31
        self.pickerToolBarFontSize=pickerToolBarFontSize;
        self.pickerFontSize=pickerFontSize;
32
        self.pickerFontFamily=pickerFontFamily;
xwenliang's avatar
xwenliang committed
33
        self.pickerFontColor=pickerFontColor;
34
        self.pickerRowHeight=pickerRowHeight;
xwenliang's avatar
xwenliang committed
35 36 37
        [self getStyle];
        [self getnumStyle];
        dispatch_async(dispatch_get_main_queue(), ^{
xwenliang's avatar
xwenliang committed
38
            [self makeuiWith:topbgColor With:bottombgColor With:leftbtnbgColor With:rightbtnbgColor With:centerbtnColor];
xwenliang's avatar
xwenliang committed
39 40 41 42 43 44 45 46
            [self selectRow];
        });
    }
    return self;
}
-(void)makeuiWith:(NSArray *)topbgColor With:(NSArray *)bottombgColor With:(NSArray *)leftbtnbgColor With:(NSArray *)rightbtnbgColor With:(NSArray *)centerbtnColor
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.frame.size.width, 40)];
47
    view.backgroundColor = [self colorWith:topbgColor];
xwenliang's avatar
xwenliang committed
48 49 50
    [self addSubview:view];
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
51
    self.leftBtn.frame = CGRectMake(0, 0, 90, 40);
52 53
    self.leftBtn.font = [UIFont fontWithName:_pickerFontFamily size:[_pickerToolBarFontSize integerValue]];
    self.leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
54
    [self.leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10.0, 0, 0)];
xwenliang's avatar
xwenliang committed
55 56
    [self.leftBtn setTitle:self.leftStr forState:UIControlStateNormal];
    [self.leftBtn setTitleColor:[self colorWith:leftbtnbgColor] forState:UIControlStateNormal];
57
    [self.leftBtn addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
xwenliang's avatar
xwenliang committed
58 59 60
    [view addSubview:self.leftBtn];
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
61
    self.rightBtn.frame = CGRectMake(view.frame.size.width-90,0, 90, 40);
62 63
    self.rightBtn.font = [UIFont fontWithName:_pickerFontFamily size:[_pickerToolBarFontSize integerValue]];
    self.rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
64
    [self.rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10.0)];
xwenliang's avatar
xwenliang committed
65 66
    [self.rightBtn setTitle:self.rightStr forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[self colorWith:rightbtnbgColor] forState:UIControlStateNormal];
xwenliang's avatar
xwenliang committed
67
    [self.rightBtn addTarget:self action:@selector(cfirmAction) forControlEvents:UIControlEventTouchUpInside];  
68
    [view addSubview:self.rightBtn];
xwenliang's avatar
xwenliang committed
69
    
xwenliang's avatar
xwenliang committed
70
    UILabel *cenLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 5, SCREEN_WIDTH-180, 30)];
xwenliang's avatar
xwenliang committed
71
    cenLabel.text=self.centStr;
72 73
    cenLabel.textAlignment=NSTextAlignmentCenter;
    cenLabel.font = [UIFont fontWithName:_pickerFontFamily size:[_pickerToolBarFontSize integerValue]];
xwenliang's avatar
xwenliang committed
74 75
    [cenLabel setTextColor:[self colorWith:centerbtnColor]];
    [view addSubview:cenLabel];
xwenliang's avatar
xwenliang committed
76

xwenliang's avatar
xwenliang committed
77
    self.pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height - 40)];
xwenliang's avatar
xwenliang committed
78 79 80 81 82 83 84 85 86 87 88
    self.pick.delegate = self;
    self.pick.dataSource = self;
    self.pick.showsSelectionIndicator=YES;
    [self addSubview:self.pick];
    
    self.pick.backgroundColor=[self colorWith:bottombgColor];
}
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (_Correlation) {
xwenliang's avatar
xwenliang committed
89
        //这里是关联的
xwenliang's avatar
xwenliang committed
90 91 92 93
        if ([_numberCorrela isEqualToString:@"three"]) {
            return 3;
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
94
            
xwenliang's avatar
xwenliang committed
95 96 97 98 99 100 101 102 103 104 105
            return 2;
        }
        
    }
    //这里是不关联的
    if (_noArryElementBool) {
        
        return 1;
        
    }else{
        
xwenliang's avatar
xwenliang committed
106
        return self.noCorreArry.count;
xwenliang's avatar
xwenliang committed
107 108 109 110 111 112 113
    }
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
114 115 116 117 118 119 120 121 122 123 124 125
        if (component == 0) {
            
            return self.provinceArray.count;
            
        } else if (component == 1) {
            
            return self.cityArray.count;
            
        } else {
            
            return self.townArray.count;
        }
xwenliang's avatar
xwenliang committed
126 127
    }
    
xwenliang's avatar
xwenliang committed
128
    //NSLog(@"%@",[self.noCorreArry objectAtIndex:component]);
xwenliang's avatar
xwenliang committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
    
    if (self.noCorreArry.count==1) {
        
        return [self.noCorreArry count];
        
    }else
    {
        
        if (_noArryElementBool) {
            
            return [self.noCorreArry count];
            
        }
        
xwenliang's avatar
xwenliang committed
143
        return  [[self.noCorreArry objectAtIndex:component] count];
xwenliang's avatar
xwenliang committed
144
    }
xwenliang's avatar
xwenliang committed
145
    
xwenliang's avatar
xwenliang committed
146 147 148 149 150 151 152 153 154
}

#pragma mark Picker Delegate Methods

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
155 156 157 158 159 160 161 162 163 164 165
        if (component == 0) {
            
            return [NSString stringWithFormat:@"%@",[self.provinceArray objectAtIndex:row]];
            
        } else if (component == 1) {
            
            return [NSString stringWithFormat:@"%@",[self.cityArray objectAtIndex:row]];
        } else {
            
            return [NSString stringWithFormat:@"%@",[self.townArray objectAtIndex:row]];
        }
xwenliang's avatar
xwenliang committed
166
    }else{
xwenliang's avatar
xwenliang committed
167
        
xwenliang's avatar
xwenliang committed
168 169 170 171 172
        if (_noArryElementBool) {
            
            return [NSString stringWithFormat:@"%@",[self.noCorreArry objectAtIndex:row]];
            
        }else{
xwenliang's avatar
xwenliang committed
173
            return [NSString stringWithFormat:@"%@",[[self.noCorreArry objectAtIndex:component] objectAtIndex:row]];
xwenliang's avatar
xwenliang committed
174 175 176 177 178 179 180
        }
    }
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    if (_Correlation) {
xwenliang's avatar
xwenliang committed
181
        if ([_numberCorrela isEqualToString:@"three"]) {
xwenliang's avatar
xwenliang committed
182
            
xwenliang's avatar
xwenliang committed
183 184
            _lineWith=SCREEN_WIDTH-2*linSpace;
            
185 186 187 188 189 190
            if (self.weightArry.count>=3) {
                NSString *onestr=[NSString stringWithFormat:@"%@",[self.weightArry firstObject]];
                NSString *twostr=[NSString stringWithFormat:@"%@",self.weightArry[1]];
                NSString *threestr=[NSString stringWithFormat:@"%@",self.weightArry[2]];
                double totalweight=onestr.doubleValue+twostr.doubleValue+threestr.doubleValue;
                if (component==0) {
xwenliang's avatar
xwenliang committed
191
                    return _lineWith*onestr.doubleValue/totalweight;
192
                }else if (component==1){
xwenliang's avatar
xwenliang committed
193
                    return _lineWith*twostr.doubleValue/totalweight;
194
                }else{
xwenliang's avatar
xwenliang committed
195
                    return _lineWith*threestr.doubleValue/totalweight;
196 197 198 199 200 201 202 203 204 205 206 207
                }
            }else{
                if (self.weightArry.count>0) {
                    NSInteger totalNum=self.weightArry.count;
                    double totalweight=0;
                    
                    for (NSInteger i=0; i<self.weightArry.count; i++) {
                        NSString *str=[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:i]];
                        totalweight=totalweight+str.doubleValue;
                    }
                    if (component>totalNum-1) {
                        NSString *str=[NSString stringWithFormat:@"%f",totalweight+3-totalNum];
xwenliang's avatar
xwenliang committed
208
                        return _lineWith/str.doubleValue;;
209 210 211 212 213
                        
                    }else{
                        
                        NSString *str=[NSString stringWithFormat:@"%f",totalweight+3-totalNum];
                        
xwenliang's avatar
xwenliang committed
214
                        return  _lineWith*[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:component]].doubleValue/str.doubleValue;
215 216 217
                        
                    }
                }else{
xwenliang's avatar
xwenliang committed
218
                    return _lineWith/3;
219 220
                }
            }
xwenliang's avatar
xwenliang committed
221 222 223 224
        }
        else{
            
            _lineWith=SCREEN_WIDTH-linSpace;
225 226 227 228 229 230
            if (self.weightArry.count>=2) {
                NSString *onestr=[NSString stringWithFormat:@"%@",[self.weightArry firstObject]];
                NSString *twostr=[NSString stringWithFormat:@"%@",self.weightArry[1]];
                
                double totalweight=onestr.doubleValue+twostr.doubleValue;
                if (component==0) {
xwenliang's avatar
xwenliang committed
231
                    return _lineWith*onestr.doubleValue/totalweight;
232
                }else{
xwenliang's avatar
xwenliang committed
233
                    return _lineWith*twostr.doubleValue/totalweight;
234
                }
xwenliang's avatar
xwenliang committed
235 236
            }
            else{
237 238 239 240 241
                if (self.weightArry.count>0) {
                    double twonum=[NSString stringWithFormat:@"%@",[self.weightArry firstObject]].doubleValue;
                    if (component==0) {
                        
                        NSString *str=[NSString stringWithFormat:@"%f",twonum+1];
xwenliang's avatar
xwenliang committed
242
                        return _lineWith*twonum/str.doubleValue;
243 244 245
                        
                    }else{
                        NSString *str=[NSString stringWithFormat:@"%f",twonum+1];
xwenliang's avatar
xwenliang committed
246
                        return _lineWith/str.doubleValue;
247 248
                        
                    }
xwenliang's avatar
xwenliang committed
249 250 251
                }
                else{
                    return _lineWith/2;
252 253
                }
            }
xwenliang's avatar
xwenliang committed
254
        }
xwenliang's avatar
xwenliang committed
255 256 257
    }else{
        if (_noArryElementBool) {
            //表示一个数组 特殊情况
xwenliang's avatar
xwenliang committed
258
            return SCREEN_WIDTH;
xwenliang's avatar
xwenliang committed
259
        }else{
xwenliang's avatar
xwenliang committed
260 261 262
            
            _lineWith=(SCREEN_WIDTH-linSpace*(self.dataDry.count-1));
            
263 264 265 266 267 268 269 270 271 272
            if (self.weightArry.count>=self.dataDry.count) {
                
                double totalweight=0;
                
                for (NSInteger i=0; i<self.dataDry.count; i++) {
                    NSString *str=[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:i]];
                    totalweight=totalweight+str.doubleValue;
                }
                NSString *comStr=[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:component]];
                
xwenliang's avatar
xwenliang committed
273
                return _lineWith*comStr.doubleValue/totalweight;
274 275 276 277 278 279 280 281 282 283 284 285
            }else
            {
                if (self.weightArry.count>0) {
                    NSInteger totalNum=self.weightArry.count;
                    double totalweight=0;
                    for (NSInteger i=0; i<self.weightArry.count; i++) {
                        NSString *str=[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:i]];
                        totalweight=totalweight+str.doubleValue;
                    }
                    if (component>totalNum-1) {
                        
                        NSString *str=[NSString stringWithFormat:@"%f",totalweight+self.dataDry.count-totalNum];
xwenliang's avatar
xwenliang committed
286
                        return _lineWith/str.doubleValue;
287 288 289
                    }else{
                        
                        NSString *str=[NSString stringWithFormat:@"%f",totalweight+self.dataDry.count-totalNum];
xwenliang's avatar
xwenliang committed
290
                        return _lineWith*[NSString stringWithFormat:@"%@",[self.weightArry objectAtIndex:component]].doubleValue/str.doubleValue;
291 292
                    }
                }else{
xwenliang's avatar
xwenliang committed
293
                    return _lineWith/self.dataDry.count;
294 295
                }
            }
xwenliang's avatar
xwenliang committed
296 297 298 299 300 301
        }
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
xwenliang's avatar
xwenliang committed
302 303 304
    if (!row) {
        row=0;
    }
xwenliang's avatar
xwenliang committed
305
    [self.backArry removeAllObjects];
xwenliang's avatar
xwenliang committed
306 307
    [self.infoArry removeAllObjects];
    
xwenliang's avatar
xwenliang committed
308 309 310
    if (_Correlation) {
        //这里是关联的
        
xwenliang's avatar
xwenliang committed
311
        if ([_numberCorrela isEqualToString:@"three"]) {
xwenliang's avatar
xwenliang committed
312
            
xwenliang's avatar
xwenliang committed
313
            if (component == 0)
xwenliang's avatar
xwenliang committed
314
            {
xwenliang's avatar
xwenliang committed
315
                [self.cityArray removeAllObjects];
xwenliang's avatar
xwenliang committed
316
                
xwenliang's avatar
xwenliang committed
317 318
                NSInteger setline=[_pick selectedRowInComponent:0];
                
xwenliang's avatar
xwenliang committed
319 320 321 322 323 324 325 326 327
                if (setline) {
                    
                    self.selectthreeAry =[[self.dataDry objectAtIndex:setline]objectForKey:[self.provinceArray objectAtIndex:setline]];
                }else{
                    
                    setline=0;
                    
                    self.selectthreeAry =[[self.dataDry objectAtIndex:0] objectForKey:[self.provinceArray objectAtIndex:0]];
                }
xwenliang's avatar
xwenliang committed
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
                
                if (self.selectthreeAry) {
                    //遍历数组
                    for (NSInteger i=0; i<self.selectthreeAry.count; i++) {
                        NSDictionary *dic=self.selectthreeAry[i];
                        NSArray *ary=[dic allKeys];
                        [self.cityArray addObject:[ary firstObject]];
                    }
                }
                else
                {
                    self.cityArray = nil;
                }
                if (self.cityArray.count > 0)
                {
                    
                    self.townArray=[[self.selectthreeAry objectAtIndex:0]objectForKey:[self.cityArray objectAtIndex:0]];
                    
                }
                else
                {
                    self.townArray = nil;
                }
                [pickerView reloadAllComponents];
                [pickerView selectRow:0 inComponent:1 animated:YES];
                [pickerView selectRow:0 inComponent:2 animated:YES];
                
xwenliang's avatar
xwenliang committed
355 356
            }
            
xwenliang's avatar
xwenliang committed
357
            if (component == 1)
xwenliang's avatar
xwenliang committed
358 359
            {
                
xwenliang's avatar
xwenliang committed
360
                NSInteger setline=[_pick selectedRowInComponent:0];
361
                
xwenliang's avatar
xwenliang committed
362 363 364 365 366
                self.selectthreeAry =[[self.dataDry objectAtIndex:setline]objectForKey:[self.provinceArray objectAtIndex:setline]];
                
                //NSLog(@"%@",_selectthreeAry);
                if (row<self.selectthreeAry.count) {
                    self.townArray=[[self.selectthreeAry objectAtIndex:row]objectForKey:[self.cityArray objectAtIndex:row]];
xwenliang's avatar
xwenliang committed
367 368 369 370
                }
                
                [pickerView reloadAllComponents];
                [pickerView selectRow:0 inComponent:2 animated:YES];
xwenliang's avatar
xwenliang committed
371 372 373
                
            }
            
xwenliang's avatar
xwenliang committed
374
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
375
            
xwenliang's avatar
xwenliang committed
376
            if (component == 0)
xwenliang's avatar
xwenliang committed
377
            {
xwenliang's avatar
xwenliang committed
378 379 380 381 382 383 384 385 386 387 388 389
                [self.cityArray removeAllObjects];
                
                self.selectArry =[[self.dataDry objectAtIndex:row]objectForKey:[self.provinceArray objectAtIndex:row]];
                
                if (self.selectArry.count>0) {
                    
                    [self.cityArray addObjectsFromArray:self.selectArry];
                }
                else
                {
                    self.cityArray = nil;
                }
xwenliang's avatar
xwenliang committed
390
            }
xwenliang's avatar
xwenliang committed
391
            [pickerView reloadComponent:1];
xwenliang's avatar
xwenliang committed
392 393 394 395 396 397
            if (component==1) {
                [pickerView selectRow:row inComponent:1 animated:YES];
                
            }else{
                [pickerView selectRow:0 inComponent:1 animated:YES];
            }
xwenliang's avatar
xwenliang committed
398 399 400 401 402 403 404 405 406 407 408 409
        }
    }
    //返回选择的值就可以了
    
    if (_Correlation) {
        
        //有关联的,里面有分两种情况
        if ([_numberCorrela isEqualToString:@"three"]) {
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
            NSString *c=[self.townArray objectAtIndex:[self.pick selectedRowInComponent:2]];
            
xwenliang's avatar
xwenliang committed
410 411 412 413 414
            if (a&&b&&c) {
                [self.backArry addObject:a];
                [self.backArry addObject:b];
                [self.backArry addObject:c];
            }
xwenliang's avatar
xwenliang committed
415 416
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
417
            
xwenliang's avatar
xwenliang committed
418 419
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
xwenliang's avatar
xwenliang committed
420
            // NSLog(@"%@---%@",a,b);
xwenliang's avatar
xwenliang committed
421 422 423 424
            if (a&&b) {
                [self.backArry addObject:a];
                [self.backArry addObject:b];
            }
xwenliang's avatar
xwenliang committed
425 426 427 428
        }
        
    }else
    {
xwenliang's avatar
xwenliang committed
429
        
xwenliang's avatar
xwenliang committed
430 431 432 433 434
        if (_noArryElementBool) {
            
            [self.backArry addObject:[self.noCorreArry objectAtIndex:row]];
            
            
xwenliang's avatar
xwenliang committed
435 436 437 438 439 440 441 442 443
        }else{
            //无关联的,直接给三个选项就行
            for (NSInteger i=0; i<self.noCorreArry.count; i++) {
                
                NSArray *eachAry=self.noCorreArry[i];
                
                [self.backArry addObject:[eachAry objectAtIndex:[self.pick selectedRowInComponent:i]]];
                
            }
xwenliang's avatar
xwenliang committed
444 445 446 447 448
        }
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setValue:self.backArry forKey:@"selectedValue"];
xwenliang's avatar
xwenliang committed
449
    
xwenliang's avatar
xwenliang committed
450
    [dic setValue:@"select" forKey:@"type"];
451 452 453 454 455 456
    NSMutableArray *value = [self getselectIndexArry];
    
    if([value count] == 0) {
        value = [[NSMutableArray alloc] init];
        [dic setValue:[NSNumber numberWithInt:[_pick selectedRowInComponent:0]] forKey:@"selectedIndex"];
    } else {
Charles-Olivier Demers's avatar
Charles-Olivier Demers committed
457
        [dic setValue:value forKey:@"selectedIndex"];
458
    }
xwenliang's avatar
xwenliang committed
459
    
xwenliang's avatar
xwenliang committed
460
    if (self.backArry.count>0) {
461
        self.bolock(dic);
xwenliang's avatar
xwenliang committed
462
    }
xwenliang's avatar
xwenliang committed
463
}
464 465 466 467 468 469

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return self.pickerRowHeight.integerValue;
}

xwenliang's avatar
xwenliang committed
470 471 472 473
//判断进来的类型是那种
-(void)getStyle
{
    
xwenliang's avatar
xwenliang committed
474 475 476
    self.dataDry=[self.pickerDic objectForKey:@"pickerData"];
    
    id firstobject=[self.dataDry firstObject];
xwenliang's avatar
xwenliang committed
477
    
478 479
    _seleNum = 1;
    
xwenliang's avatar
xwenliang committed
480
    if ([firstobject isKindOfClass:[NSArray class]]) {
xwenliang's avatar
xwenliang committed
481
        
xwenliang's avatar
xwenliang committed
482 483
        _seleNum=self.dataDry.count;
        
xwenliang's avatar
xwenliang committed
484 485
        _Correlation=NO;
        
xwenliang's avatar
xwenliang committed
486
    }else if ([firstobject isKindOfClass:[NSDictionary class]]){
xwenliang's avatar
xwenliang committed
487
        
xwenliang's avatar
xwenliang committed
488
        //_Correlation为YES的话是关联的情况 为NO的话 是不关联的情况
xwenliang's avatar
xwenliang committed
489 490
        _Correlation=YES;
        
xwenliang's avatar
xwenliang committed
491
        NSDictionary *dic=(NSDictionary *)firstobject;
xwenliang's avatar
xwenliang committed
492
        
xwenliang's avatar
xwenliang committed
493
        NSArray * twoOrthree=[dic allKeys];
xwenliang's avatar
xwenliang committed
494
        
xwenliang's avatar
xwenliang committed
495 496 497 498
        
        id scendObjct=[[dic objectForKey:[twoOrthree firstObject]] firstObject];
        
        if ([scendObjct isKindOfClass:[NSDictionary class]]) {
xwenliang's avatar
xwenliang committed
499 500
            
            _numberCorrela=@"three";
xwenliang's avatar
xwenliang committed
501
            _seleNum=3;
xwenliang's avatar
xwenliang committed
502
            
xwenliang's avatar
xwenliang committed
503
        }else{
xwenliang's avatar
xwenliang committed
504
            _numberCorrela=@"two";
xwenliang's avatar
xwenliang committed
505
            _seleNum=2;
xwenliang's avatar
xwenliang committed
506 507 508 509
        }
    }
}
-(void)getnumStyle{
xwenliang's avatar
xwenliang committed
510
    
xwenliang's avatar
xwenliang committed
511 512
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
513 514 515 516 517 518 519 520
        //这里是关联的
        if ([_numberCorrela isEqualToString:@"three"]) {
            //省 市
            for (NSInteger i=0; i<self.dataDry.count; i++) {
                
                NSDictionary *dic=[self.dataDry objectAtIndex:i];
                
                NSArray *ary=[dic allKeys];
521 522 523
                if ([ary firstObject]) {
                    [self.provinceArray addObject:[ary firstObject]];
                }
xwenliang's avatar
xwenliang committed
524
            }
xwenliang's avatar
xwenliang committed
525
            
xwenliang's avatar
xwenliang committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
            NSDictionary *dic=[self.dataDry firstObject];
            
            NSArray *ary=[dic objectForKey:[self.provinceArray objectAtIndex:0]];
            
            
            if (self.provinceArray.count > 0) {
                
                for (NSInteger i=0; i<ary.count; i++) {
                    
                    NSDictionary *dic=[ary objectAtIndex:i];
                    
                    NSArray *ary=[dic allKeys];
                    
                    [self.cityArray addObject:[ary firstObject]];
                    
                }
            }
xwenliang's avatar
xwenliang committed
543
            
xwenliang's avatar
xwenliang committed
544 545 546 547 548 549 550 551
            if (self.cityArray.count > 0) {
                
                NSDictionary *dic=[ary firstObject];
                
                self.townArray=[dic objectForKey:[self.cityArray firstObject]];
                
            }
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
552
            
xwenliang's avatar
xwenliang committed
553 554 555 556 557 558 559 560 561
            for (NSInteger i=0; i<self.dataDry.count; i++) {
                
                NSDictionary *dic=[self.dataDry objectAtIndex:i];
                
                NSArray *ary=[dic allKeys];
                
                [self.provinceArray addObject:[ary firstObject]];
            }
            [self.cityArray addObjectsFromArray:[[self.dataDry objectAtIndex:0] objectForKey:[self.provinceArray objectAtIndex:0]]];
xwenliang's avatar
xwenliang committed
562
        }
xwenliang's avatar
xwenliang committed
563 564 565 566
    }else
    {
        //这里是不关联的
        self.noCorreArry=self.dataDry;
xwenliang's avatar
xwenliang committed
567
        
xwenliang's avatar
xwenliang committed
568
        id noArryElement=[self.dataDry firstObject];
xwenliang's avatar
xwenliang committed
569
        
xwenliang's avatar
xwenliang committed
570 571 572 573 574 575 576 577
        if ([noArryElement isKindOfClass:[NSArray class]]) {
            
            _noArryElementBool=NO;
            
        }else{
            //这里为yes表示里面就就一行数据 表示的是只有一行的特殊情况
            _noArryElementBool=YES;
        }
xwenliang's avatar
xwenliang committed
578 579 580 581 582 583 584 585 586 587 588 589
    }
}

//按了取消按钮
-(void)cancleAction
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    
    if (self.backArry.count>0) {
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"cancel" forKey:@"type"];
        
xwenliang's avatar
xwenliang committed
590 591
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        
xwenliang's avatar
xwenliang committed
592 593 594 595 596 597
        self.bolock(dic);
    }else{
        [self getNOselectinfo];
        
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"cancel" forKey:@"type"];
xwenliang's avatar
xwenliang committed
598
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
xwenliang's avatar
xwenliang committed
599 600
        self.bolock(dic);
    }
xwenliang's avatar
xwenliang committed
601 602
    
    
xwenliang's avatar
xwenliang committed
603 604 605 606 607 608 609
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.2f animations:^{
            
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250)];
            
        }];
    });
610 611

    self.pick.hidden=YES;
xwenliang's avatar
xwenliang committed
612 613 614 615
}
//按了确定按钮
-(void)cfirmAction
{
616 617 618 619 620
    //判断当前是否在滚动选择 如果是 则禁用确定按钮
    if ([self anySubViewScrolling:self.pick]) {
        return ;
    }
    
xwenliang's avatar
xwenliang committed
621 622 623 624 625 626
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    
    if (self.backArry.count>0) {
        
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"confirm" forKey:@"type"];
xwenliang's avatar
xwenliang committed
627 628
        NSMutableArray *arry=[[NSMutableArray alloc]init];
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
629
//        [dic setValue:arry forKey:@"selectedIndex"];
xwenliang's avatar
xwenliang committed
630 631 632 633 634 635 636 637
        
        self.bolock(dic);
        
    }else{
        [self getNOselectinfo];
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"confirm" forKey:@"type"];
        
xwenliang's avatar
xwenliang committed
638 639
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        
xwenliang's avatar
xwenliang committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
        self.bolock(dic);
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.2f animations:^{
            
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250)];
        }];
    });
}
-(void)selectRow
{
    if (_Correlation) {
        //关联的一开始的默认选择行数
        if ([_numberCorrela isEqualToString:@"three"]) {
xwenliang's avatar
xwenliang committed
655 656 657
            
            [self selectValueThree];
            
xwenliang's avatar
xwenliang committed
658 659
        }else if ([_numberCorrela isEqualToString:@"two"]){
            
xwenliang's avatar
xwenliang committed
660
            [self selectValueTwo];
xwenliang's avatar
xwenliang committed
661 662 663 664 665 666 667
        }
    }else{
        //一行的时候
        [self selectValueOne];
    }
}
//三行时候的选择哪个的逻辑
xwenliang's avatar
xwenliang committed
668
-(void)selectValueThree
xwenliang's avatar
xwenliang committed
669 670 671 672 673 674
{
    NSString *selectStr=[NSString stringWithFormat:@"%@",self.selectValueArry.firstObject];
    
    for (NSInteger i=0; i<self.provinceArray.count; i++) {
        NSString *str=[NSString stringWithFormat:@"%@",[self.provinceArray objectAtIndex:i]];
        if ([selectStr isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
675
            _num=i;
xwenliang's avatar
xwenliang committed
676
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
677
            
xwenliang's avatar
xwenliang committed
678 679 680 681 682
            [_pick selectRow:i  inComponent:0 animated:NO];
            break;
        }
    }
    
xwenliang's avatar
xwenliang committed
683
    NSArray *selecityAry = [[self.dataDry objectAtIndex:_num] objectForKey:selectStr];
xwenliang's avatar
xwenliang committed
684 685 686
    
    if (selecityAry.count>0) {
        
xwenliang's avatar
xwenliang committed
687 688 689 690 691 692 693 694 695 696
        [self.cityArray removeAllObjects];
        
        for (NSInteger i=0; i<selecityAry.count; i++) {
            
            NSDictionary *dic=selecityAry[i];
            
            NSArray *ary=[dic allKeys];
            
            [self.cityArray addObject:[ary firstObject]];
        }
xwenliang's avatar
xwenliang committed
697
    }
xwenliang's avatar
xwenliang committed
698
    
xwenliang's avatar
xwenliang committed
699 700 701
    NSString *selectStrTwo;
    
    if (self.selectValueArry.count>1) {
xwenliang's avatar
xwenliang committed
702
        
xwenliang's avatar
xwenliang committed
703 704 705 706 707 708
        selectStrTwo=[NSString stringWithFormat:@"%@",self.selectValueArry[1]];
    }
    for (NSInteger i=0; i<self.cityArray.count; i++) {
        
        NSString *str=[NSString stringWithFormat:@"%@",[self.cityArray objectAtIndex:i]];
        if ([selectStrTwo isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
709 710 711
            
            _threenum=i;
            
xwenliang's avatar
xwenliang committed
712
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
713
            
xwenliang's avatar
xwenliang committed
714 715 716 717 718 719
            [_pick selectRow:i  inComponent:1 animated:NO];
            
            break;
        }
    }
    
xwenliang's avatar
xwenliang committed
720
    if (selecityAry.count>0) {
xwenliang's avatar
xwenliang committed
721 722 723
        
        if (self.selectValueArry.count>1) {
            
xwenliang's avatar
xwenliang committed
724 725
            NSArray *arry =[[selecityAry objectAtIndex:_threenum] objectForKey:[self.selectValueArry objectAtIndex:1]];
            
xwenliang's avatar
xwenliang committed
726 727
            self.townArray=arry;
            
xwenliang's avatar
xwenliang committed
728 729 730 731 732
        }
    }
    
    NSString *selectStrThree;
    
xwenliang's avatar
xwenliang committed
733
    
xwenliang's avatar
xwenliang committed
734 735 736
    if (self.selectValueArry.count>2) {
        selectStrThree=[NSString stringWithFormat:@"%@",self.selectValueArry[2]];
    }
xwenliang's avatar
xwenliang committed
737 738
    
    if (self.townArray.count>0) {
xwenliang's avatar
xwenliang committed
739
        
xwenliang's avatar
xwenliang committed
740 741 742 743 744 745 746 747 748 749 750
        for (NSInteger i=0; i<self.townArray.count; i++) {
            
            NSString *str=[NSString stringWithFormat:@"%@",[self.townArray objectAtIndex:i]];
            if ([selectStrThree isEqualToString:str]) {
                
                [_pick reloadAllComponents];
                
                [_pick selectRow:i  inComponent:2 animated:NO];
                
                break;
            }
xwenliang's avatar
xwenliang committed
751
        }
xwenliang's avatar
xwenliang committed
752 753 754 755
    }else
    {
        NSArray *threekey=[[selecityAry objectAtIndex:0]allKeys];
        self.townArray=[[selecityAry objectAtIndex:0]objectForKey:[threekey firstObject]];
xwenliang's avatar
xwenliang committed
756
    }
xwenliang's avatar
xwenliang committed
757
    [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
758 759
}
//两行时候的选择哪个的逻辑
xwenliang's avatar
xwenliang committed
760
-(void)selectValueTwo
xwenliang's avatar
xwenliang committed
761 762 763 764 765 766 767
{
    
    NSString *selectStr=[NSString stringWithFormat:@"%@",self.selectValueArry.firstObject];
    
    for (NSInteger i=0; i<self.provinceArray.count; i++) {
        NSString *str=[NSString stringWithFormat:@"%@",[self.provinceArray objectAtIndex:i]];
        if ([selectStr isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
768
            
xwenliang's avatar
xwenliang committed
769
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
770
            [_pick selectRow:i  inComponent:0 animated:NO];
xwenliang's avatar
xwenliang committed
771
            _num=i;
xwenliang's avatar
xwenliang committed
772 773 774
            break;
        }
    }
xwenliang's avatar
xwenliang committed
775 776 777 778 779 780
    NSArray *twoArry=[[self.dataDry objectAtIndex:_num]objectForKey:selectStr];
    
    if (twoArry&&twoArry.count>0) {
        
        [self.cityArray removeAllObjects];
        [self.cityArray addObjectsFromArray:twoArry];
xwenliang's avatar
xwenliang committed
781
    }
xwenliang's avatar
xwenliang committed
782
    
xwenliang's avatar
xwenliang committed
783 784
    NSString *selectTwoStr;
    if (self.selectValueArry.count>1) {
xwenliang's avatar
xwenliang committed
785
        
xwenliang's avatar
xwenliang committed
786 787 788 789 790 791
        selectTwoStr =[NSString stringWithFormat:@"%@",[self.selectValueArry objectAtIndex:1]];
    }
    
    for (NSInteger i=0; i<self.cityArray.count; i++) {
        
        NSString *str=[NSString stringWithFormat:@"%@",[self.cityArray objectAtIndex:i]];
xwenliang's avatar
xwenliang committed
792
        
xwenliang's avatar
xwenliang committed
793 794
        if ([selectTwoStr isEqualToString:str]) {
            
xwenliang's avatar
xwenliang committed
795
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
            [_pick selectRow:i inComponent:1 animated:NO];
            
            break;
        }
    }
}
//一行时候的选择哪个的逻辑
-(void)selectValueOne
{
    if (_noArryElementBool) {
        //这里表示数组里面就只有一个数组 比较特殊的情况[]
        NSString *selectStr;
        if (self.selectValueArry.count>0) {
            
            selectStr=[NSString stringWithFormat:@"%@",[self.selectValueArry firstObject]];
        }
        for (NSInteger i=0; i<self.noCorreArry.count; i++) {
            NSString *str=[NSString stringWithFormat:@"%@",[self.noCorreArry objectAtIndex:i]];
            if ([selectStr isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
815
                [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
                [_pick selectRow:i  inComponent:0 animated:NO];
                break;
            }
        }
        
    }else{
        //这里就比较复杂了 [[],[],[]]
        if (self.selectValueArry.count>0) {
            
            if (self.selectValueArry.count>self.noCorreArry.count) {
                
                for (NSInteger i=0; i<self.noCorreArry.count; i++) {
                    
                    NSString *selectStr=[NSString stringWithFormat:@"%@",[self.selectValueArry objectAtIndex:i]];
                    
                    NSArray *arry=[self.noCorreArry objectAtIndex:i];
                    
                    for (NSInteger j=0; j<arry.count; j++) {
                        
                        NSString *str=[NSString stringWithFormat:@"%@",[arry objectAtIndex:j]];
                        
                        if ([selectStr isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
838
                            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
                            [_pick selectRow:j inComponent:i animated:YES];
                            
                            break;
                        }
                    }
                }
            }else{
                for (NSInteger i=0; i<self.selectValueArry.count; i++) {
                    
                    NSString *selectStr=[NSString stringWithFormat:@"%@",[self.selectValueArry objectAtIndex:i]];
                    
                    NSArray *arry=[self.noCorreArry objectAtIndex:i];
                    
                    for (NSInteger j=0; j<arry.count; j++) {
                        
                        NSString *str=[NSString stringWithFormat:@"%@",[arry objectAtIndex:j]];
                        
                        if ([selectStr isEqualToString:str]) {
xwenliang's avatar
xwenliang committed
857
                            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
                            [_pick selectRow:j inComponent:i animated:YES];
                            
                            break;
                        }
                    }
                }
            }
        }
    }
}
-(void)getNOselectinfo
{
    if (_Correlation) {
        
        //有关联的,里面有分两种情况
        if ([_numberCorrela isEqualToString:@"three"]) {
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
            NSString *c=[self.townArray objectAtIndex:[self.pick selectedRowInComponent:2]];
            
            [self.backArry addObject:a];
            [self.backArry addObject:b];
            [self.backArry addObject:c];
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
            
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
xwenliang's avatar
xwenliang committed
886
            //NSLog(@"%@---%@",a,b);
xwenliang's avatar
xwenliang committed
887 888 889 890 891 892 893 894 895 896 897 898 899
            [self.backArry addObject:a];
            [self.backArry addObject:b];
        }
        
    }else
    {
        
        if (_noArryElementBool) {
            
            if (self.selectValueArry.count>0) {
                NSString *selectStr=[NSString stringWithFormat:@"%@",[self.selectValueArry firstObject]];
                [self.backArry addObject:selectStr];
            }else{
xwenliang's avatar
xwenliang committed
900 901
                
                [self.backArry addObject:[self.noCorreArry objectAtIndex:0]];
xwenliang's avatar
xwenliang committed
902 903 904
            }
            
        }else{
xwenliang's avatar
xwenliang committed
905
            //无关联的,直接给几个选项就行
xwenliang's avatar
xwenliang committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
            for (NSInteger i=0; i<self.noCorreArry.count; i++) {
                
                NSArray *eachAry=self.noCorreArry[i];
                
                [self.backArry addObject:[eachAry objectAtIndex:[self.pick selectedRowInComponent:i]]];
                
            }
        }
    }
}

-(UIColor *)colorWith:(NSArray *)colorArry
{
    NSString *ColorA=[NSString stringWithFormat:@"%@",colorArry[0]];
    NSString *ColorB=[NSString stringWithFormat:@"%@",colorArry[1]];
    NSString *ColorC=[NSString stringWithFormat:@"%@",colorArry[2]];
    NSString *ColorD=[NSString stringWithFormat:@"%@",colorArry[3]];
    
    UIColor *color=[[UIColor alloc]initWithRed:[ColorA integerValue]/255.0 green:[ColorB integerValue]/255.0 blue:[ColorC integerValue]/255.0 alpha:[ColorD floatValue]];
    return color;
}
xwenliang's avatar
xwenliang committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
-(NSArray *)getselectIndexArry{
    
    NSMutableArray *arry=[[NSMutableArray alloc]init];
    for (NSInteger i=0; i<_seleNum; i++) {
        NSNumber *num=[[NSNumber alloc]initWithInteger:[self.pick selectedRowInComponent:i]];
        [arry addObject:num];
        
    }
    return arry;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *lbl = (UILabel *)view;
    
    if (lbl == nil) {
        lbl = [[UILabel alloc]init];
944
        lbl.font = [UIFont fontWithName:_pickerFontFamily size:[_pickerFontSize integerValue]];
xwenliang's avatar
xwenliang committed
945 946 947 948 949 950 951 952 953 954
        lbl.textColor = [self colorWith:_pickerFontColor];
        lbl.textAlignment = UITextAlignmentCenter;
    }
    
    //重新加载lbl的文字内容
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return lbl;
    
}
xwenliang's avatar
xwenliang committed
955

956 957 958 959 960 961 962 963 964 965 966 967 968 969
- (BOOL)anySubViewScrolling:(UIView *)view{
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)view;
        if (scrollView.dragging || scrollView.decelerating) {
            return YES;
        }
    }
    for (UIView *theSubView in view.subviews) {
        if ([self anySubViewScrolling:theSubView]) {
            return YES;
        }
    }
    return NO;
}
xwenliang's avatar
xwenliang committed
970
@end