BzwPicker.m 32.7 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
xwenliang's avatar
xwenliang committed
15 16
       pickerToolBarFontSize:(NSString *)pickerToolBarFontSize  pickerFontSize:(NSString *)pickerFontSize  pickerFontColor:(NSArray *)pickerFontColor

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 32
        self.pickerToolBarFontSize=pickerToolBarFontSize;
        self.pickerFontSize=pickerFontSize;
        self.pickerFontColor=pickerFontColor;
xwenliang's avatar
xwenliang committed
33 34 35
        [self getStyle];
        [self getnumStyle];
        dispatch_async(dispatch_get_main_queue(), ^{
xwenliang's avatar
xwenliang committed
36
            [self makeuiWith:topbgColor With:bottombgColor With:leftbtnbgColor With:rightbtnbgColor With:centerbtnColor];
xwenliang's avatar
xwenliang committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
            [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)];
    view.backgroundColor = [UIColor cyanColor];
    
    [self addSubview:view];
    
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
xwenliang's avatar
xwenliang committed
51
    self.leftBtn.frame = CGRectMake(10, 5, 90, 30);
xwenliang's avatar
xwenliang committed
52
    [self.leftBtn setTitle:self.leftStr forState:UIControlStateNormal];
xwenliang's avatar
xwenliang committed
53
    [self.leftBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
54 55 56 57 58 59 60 61 62 63
    self.leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.leftBtn addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.leftBtn setTitleColor:[self colorWith:leftbtnbgColor] forState:UIControlStateNormal];
    
    [view addSubview:self.leftBtn];
    
    view.backgroundColor=[self colorWith:topbgColor];
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
xwenliang's avatar
xwenliang committed
64
    self.rightBtn.frame = CGRectMake(view.frame.size.width-100,5, 90, 30);
xwenliang's avatar
xwenliang committed
65 66 67 68 69 70 71
    [self.rightBtn setTitle:self.rightStr forState:UIControlStateNormal];
    self.rightBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    
    [self.rightBtn setTitleColor:[self colorWith:rightbtnbgColor] forState:UIControlStateNormal];
    
    
    [view addSubview:self.rightBtn];
xwenliang's avatar
xwenliang committed
72
    [self.rightBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
73 74 75
    [self.rightBtn addTarget:self action:@selector(cfirmAction) forControlEvents:UIControlEventTouchUpInside];
    
    
xwenliang's avatar
xwenliang committed
76 77 78
    UILabel *cenLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 5, SCREEN_WIDTH-180, 30)];
    
    cenLabel.textAlignment=NSTextAlignmentCenter;
xwenliang's avatar
xwenliang committed
79
    
xwenliang's avatar
xwenliang committed
80
    [cenLabel setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
81 82
    
    cenLabel.text=self.centStr;
xwenliang's avatar
xwenliang committed
83
    
xwenliang's avatar
xwenliang committed
84
    [cenLabel setTextColor:[self colorWith:centerbtnColor]];
xwenliang's avatar
xwenliang committed
85
    
xwenliang's avatar
xwenliang committed
86
    [view addSubview:cenLabel];
xwenliang's avatar
xwenliang committed
87 88
    
    self.pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height - 40)];
xwenliang's avatar
xwenliang committed
89 90 91 92 93 94 95 96 97 98 99 100
    
    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
101
        //这里是关联的
xwenliang's avatar
xwenliang committed
102 103 104 105 106
        if ([_numberCorrela isEqualToString:@"three"]) {
            
            return 3;
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
107
            
xwenliang's avatar
xwenliang committed
108 109 110 111 112 113 114 115 116 117 118
            return 2;
        }
        
    }
    //这里是不关联的
    if (_noArryElementBool) {
        
        return 1;
        
    }else{
        
xwenliang's avatar
xwenliang committed
119
        return self.noCorreArry.count;
xwenliang's avatar
xwenliang committed
120 121 122 123 124 125 126
    }
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
127 128 129 130 131 132 133 134 135 136 137 138
        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
139 140
    }
    
xwenliang's avatar
xwenliang committed
141
    //NSLog(@"%@",[self.noCorreArry objectAtIndex:component]);
xwenliang's avatar
xwenliang committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
    
    if (self.noCorreArry.count==1) {
        
        return [self.noCorreArry count];
        
    }else
    {
        
        if (_noArryElementBool) {
            
            return [self.noCorreArry count];
            
        }
        
xwenliang's avatar
xwenliang committed
156
        return  [[self.noCorreArry objectAtIndex:component] count];
xwenliang's avatar
xwenliang committed
157
    }
xwenliang's avatar
xwenliang committed
158
    
xwenliang's avatar
xwenliang committed
159 160 161 162 163 164 165 166 167
}

#pragma mark Picker Delegate Methods

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
xwenliang's avatar
xwenliang committed
314 315 316
    if (!row) {
        row=0;
    }
xwenliang's avatar
xwenliang committed
317
    [self.backArry removeAllObjects];
xwenliang's avatar
xwenliang committed
318 319
    [self.infoArry removeAllObjects];
    
xwenliang's avatar
xwenliang committed
320 321 322
    if (_Correlation) {
        //这里是关联的
        
xwenliang's avatar
xwenliang committed
323
        if ([_numberCorrela isEqualToString:@"three"]) {
xwenliang's avatar
xwenliang committed
324
            
xwenliang's avatar
xwenliang committed
325
            if (component == 0)
xwenliang's avatar
xwenliang committed
326
            {
xwenliang's avatar
xwenliang committed
327
                [self.cityArray removeAllObjects];
xwenliang's avatar
xwenliang committed
328
                
xwenliang's avatar
xwenliang committed
329 330 331
                NSInteger setline=[_pick selectedRowInComponent:0];
                
                if (setline) {
xwenliang's avatar
xwenliang committed
332
                    
xwenliang's avatar
xwenliang committed
333
                    self.selectthreeAry =[[self.dataDry objectAtIndex:setline]objectForKey:[self.provinceArray objectAtIndex:setline]];
xwenliang's avatar
xwenliang committed
334 335
                }else{
                    
xwenliang's avatar
xwenliang committed
336
                    setline=0;
xwenliang's avatar
xwenliang committed
337
                    
xwenliang's avatar
xwenliang committed
338
                    self.selectthreeAry =[[self.dataDry objectAtIndex:0] objectForKey:[self.provinceArray objectAtIndex:0]];
xwenliang's avatar
xwenliang committed
339
                }
xwenliang's avatar
xwenliang committed
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
                
                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
367 368
            }
            
xwenliang's avatar
xwenliang committed
369
            if (component == 1)
xwenliang's avatar
xwenliang committed
370 371
            {
                
xwenliang's avatar
xwenliang committed
372 373 374 375 376 377
                NSInteger setline=[_pick selectedRowInComponent:0];
                
                if (setline) {
                    
                    self.selectthreeAry =[[self.dataDry objectAtIndex:setline]objectForKey:[self.provinceArray objectAtIndex:setline]];
                    
xwenliang's avatar
xwenliang committed
378
                    //NSLog(@"%@",_selectthreeAry);
xwenliang's avatar
xwenliang committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392
                    self.townArray=[[self.selectthreeAry  objectAtIndex:row]objectForKey:[self.cityArray objectAtIndex:row]];
                    
                }else{
                    
                    setline=0;
                    
                    self.selectthreeAry =[[self.dataDry objectAtIndex:0] objectForKey:[self.provinceArray objectAtIndex:0]];
                    
                    //NSLog(@"%ld",(long)row);
                    self.townArray=[[self.selectthreeAry objectAtIndex:row]objectForKey:[self.cityArray objectAtIndex:row]];
                }
                
                [pickerView reloadAllComponents];
                [pickerView selectRow:0 inComponent:2 animated:YES];
xwenliang's avatar
xwenliang committed
393 394 395
                
            }
            
xwenliang's avatar
xwenliang committed
396
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
397
            
xwenliang's avatar
xwenliang committed
398
            if (component == 0)
xwenliang's avatar
xwenliang committed
399
            {
xwenliang's avatar
xwenliang committed
400 401 402 403 404 405 406 407 408 409 410 411
                [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
412
            }
xwenliang's avatar
xwenliang committed
413
            [pickerView reloadComponent:1];
xwenliang's avatar
xwenliang committed
414 415 416 417 418 419
            if (component==1) {
                [pickerView selectRow:row inComponent:1 animated:YES];
                
            }else{
                [pickerView selectRow:0 inComponent:1 animated:YES];
            }
xwenliang's avatar
xwenliang committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        }
    }
    //返回选择的值就可以了
    
    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"]){
xwenliang's avatar
xwenliang committed
437
            
xwenliang's avatar
xwenliang committed
438 439
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
xwenliang's avatar
xwenliang committed
440
            // NSLog(@"%@---%@",a,b);
xwenliang's avatar
xwenliang committed
441 442 443 444 445 446
            [self.backArry addObject:a];
            [self.backArry addObject:b];
        }
        
    }else
    {
xwenliang's avatar
xwenliang committed
447
        
xwenliang's avatar
xwenliang committed
448 449 450 451 452
        if (_noArryElementBool) {
            
            [self.backArry addObject:[self.noCorreArry objectAtIndex:row]];
            
            
xwenliang's avatar
xwenliang committed
453 454 455 456 457 458 459 460 461
        }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
462 463 464 465 466
        }
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setValue:self.backArry forKey:@"selectedValue"];
xwenliang's avatar
xwenliang committed
467
    
xwenliang's avatar
xwenliang committed
468 469
    [dic setValue:@"select" forKey:@"type"];
    
xwenliang's avatar
xwenliang committed
470 471
    [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
    
xwenliang's avatar
xwenliang committed
472 473 474 475 476 477
    self.bolock(dic);
}
//判断进来的类型是那种
-(void)getStyle
{
    
xwenliang's avatar
xwenliang committed
478 479 480
    self.dataDry=[self.pickerDic objectForKey:@"pickerData"];
    
    id firstobject=[self.dataDry firstObject];
xwenliang's avatar
xwenliang committed
481
    
xwenliang's avatar
xwenliang committed
482
    if ([firstobject isKindOfClass:[NSArray class]]) {
xwenliang's avatar
xwenliang committed
483
        
xwenliang's avatar
xwenliang committed
484 485
        _seleNum=self.dataDry.count;
        
xwenliang's avatar
xwenliang committed
486 487
        _Correlation=NO;
        
xwenliang's avatar
xwenliang committed
488
    }else if ([firstobject isKindOfClass:[NSDictionary class]]){
xwenliang's avatar
xwenliang committed
489
        
xwenliang's avatar
xwenliang committed
490
        //_Correlation为YES的话是关联的情况 为NO的话 是不关联的情况
xwenliang's avatar
xwenliang committed
491 492
        _Correlation=YES;
        
xwenliang's avatar
xwenliang committed
493
        NSDictionary *dic=(NSDictionary *)firstobject;
xwenliang's avatar
xwenliang committed
494
        
xwenliang's avatar
xwenliang committed
495
        NSArray * twoOrthree=[dic allKeys];
xwenliang's avatar
xwenliang committed
496
        
xwenliang's avatar
xwenliang committed
497 498 499 500
        
        id scendObjct=[[dic objectForKey:[twoOrthree firstObject]] firstObject];
        
        if ([scendObjct isKindOfClass:[NSDictionary class]]) {
xwenliang's avatar
xwenliang committed
501 502
            
            _numberCorrela=@"three";
xwenliang's avatar
xwenliang committed
503
            _seleNum=3;
xwenliang's avatar
xwenliang committed
504
            
xwenliang's avatar
xwenliang committed
505
        }else{
xwenliang's avatar
xwenliang committed
506
            _numberCorrela=@"two";
xwenliang's avatar
xwenliang committed
507
            _seleNum=2;
xwenliang's avatar
xwenliang committed
508 509 510 511
        }
    }
}
-(void)getnumStyle{
xwenliang's avatar
xwenliang committed
512
    
xwenliang's avatar
xwenliang committed
513 514
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
515 516 517 518 519 520 521 522 523 524 525
        //这里是关联的
        if ([_numberCorrela isEqualToString:@"three"]) {
            //省 市
            for (NSInteger i=0; i<self.dataDry.count; i++) {
                
                NSDictionary *dic=[self.dataDry objectAtIndex:i];
                
                NSArray *ary=[dic allKeys];
                
                [self.provinceArray addObject:[ary firstObject]];
            }
xwenliang's avatar
xwenliang committed
526
            
xwenliang's avatar
xwenliang committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            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
544
            
xwenliang's avatar
xwenliang committed
545 546 547 548 549 550 551 552
            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
553
            
xwenliang's avatar
xwenliang committed
554 555 556 557 558 559 560 561 562
            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
563
        }
xwenliang's avatar
xwenliang committed
564 565 566 567
    }else
    {
        //这里是不关联的
        self.noCorreArry=self.dataDry;
xwenliang's avatar
xwenliang committed
568
        
xwenliang's avatar
xwenliang committed
569
        id noArryElement=[self.dataDry firstObject];
xwenliang's avatar
xwenliang committed
570
        
xwenliang's avatar
xwenliang committed
571 572 573 574 575 576 577 578
        if ([noArryElement isKindOfClass:[NSArray class]]) {
            
            _noArryElementBool=NO;
            
        }else{
            //这里为yes表示里面就就一行数据 表示的是只有一行的特殊情况
            _noArryElementBool=YES;
        }
xwenliang's avatar
xwenliang committed
579 580 581 582 583 584 585 586 587 588 589 590
    }
}

//按了取消按钮
-(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
591 592
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        
xwenliang's avatar
xwenliang committed
593 594 595 596 597 598
        self.bolock(dic);
    }else{
        [self getNOselectinfo];
        
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"cancel" forKey:@"type"];
xwenliang's avatar
xwenliang committed
599
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
xwenliang's avatar
xwenliang committed
600 601
        self.bolock(dic);
    }
xwenliang's avatar
xwenliang committed
602 603
    
    
xwenliang's avatar
xwenliang committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.2f animations:^{
            
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250)];
            
        }];
    });
}
//按了确定按钮
-(void)cfirmAction
{
    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
621 622 623
        NSMutableArray *arry=[[NSMutableArray alloc]init];
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        [dic setValue:arry forKey:@"selectedIndex"];
xwenliang's avatar
xwenliang committed
624 625 626 627 628 629 630 631
        
        self.bolock(dic);
        
    }else{
        [self getNOselectinfo];
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"confirm" forKey:@"type"];
        
xwenliang's avatar
xwenliang committed
632 633
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        
xwenliang's avatar
xwenliang committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
        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
649 650 651
            
            [self selectValueThree];
            
xwenliang's avatar
xwenliang committed
652 653
        }else if ([_numberCorrela isEqualToString:@"two"]){
            
xwenliang's avatar
xwenliang committed
654
            [self selectValueTwo];
xwenliang's avatar
xwenliang committed
655 656 657 658 659 660 661
        }
    }else{
        //一行的时候
        [self selectValueOne];
    }
}
//三行时候的选择哪个的逻辑
xwenliang's avatar
xwenliang committed
662
-(void)selectValueThree
xwenliang's avatar
xwenliang committed
663 664 665 666 667 668
{
    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
669
            _num=i;
xwenliang's avatar
xwenliang committed
670
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
671
            
xwenliang's avatar
xwenliang committed
672 673 674 675 676
            [_pick selectRow:i  inComponent:0 animated:NO];
            break;
        }
    }
    
xwenliang's avatar
xwenliang committed
677
    NSArray *selecityAry = [[self.dataDry objectAtIndex:_num] objectForKey:selectStr];
xwenliang's avatar
xwenliang committed
678 679 680
    
    if (selecityAry.count>0) {
        
xwenliang's avatar
xwenliang committed
681 682 683 684 685 686 687 688 689 690
        [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
691
    }
xwenliang's avatar
xwenliang committed
692
    
xwenliang's avatar
xwenliang committed
693 694 695
    NSString *selectStrTwo;
    
    if (self.selectValueArry.count>1) {
xwenliang's avatar
xwenliang committed
696
        
xwenliang's avatar
xwenliang committed
697 698 699 700 701 702
        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
703 704 705
            
            _threenum=i;
            
xwenliang's avatar
xwenliang committed
706
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
707
            
xwenliang's avatar
xwenliang committed
708 709 710 711 712 713
            [_pick selectRow:i  inComponent:1 animated:NO];
            
            break;
        }
    }
    
xwenliang's avatar
xwenliang committed
714
    if (selecityAry.count>0) {
xwenliang's avatar
xwenliang committed
715 716 717
        
        if (self.selectValueArry.count>1) {
            
xwenliang's avatar
xwenliang committed
718 719
            NSArray *arry =[[selecityAry objectAtIndex:_threenum] objectForKey:[self.selectValueArry objectAtIndex:1]];
            
xwenliang's avatar
xwenliang committed
720 721
            self.townArray=arry;
            
xwenliang's avatar
xwenliang committed
722 723 724 725 726
        }
    }
    
    NSString *selectStrThree;
    
xwenliang's avatar
xwenliang committed
727
    
xwenliang's avatar
xwenliang committed
728 729 730
    if (self.selectValueArry.count>2) {
        selectStrThree=[NSString stringWithFormat:@"%@",self.selectValueArry[2]];
    }
xwenliang's avatar
xwenliang committed
731 732
    
    if (self.townArray.count>0) {
xwenliang's avatar
xwenliang committed
733
        
xwenliang's avatar
xwenliang committed
734 735 736 737 738 739 740 741 742 743 744
        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
745
        }
xwenliang's avatar
xwenliang committed
746 747 748 749
    }else
    {
        NSArray *threekey=[[selecityAry objectAtIndex:0]allKeys];
        self.townArray=[[selecityAry objectAtIndex:0]objectForKey:[threekey firstObject]];
xwenliang's avatar
xwenliang committed
750
    }
xwenliang's avatar
xwenliang committed
751
    [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
752 753
}
//两行时候的选择哪个的逻辑
xwenliang's avatar
xwenliang committed
754
-(void)selectValueTwo
xwenliang's avatar
xwenliang committed
755 756 757 758 759 760 761
{
    
    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
762
            
xwenliang's avatar
xwenliang committed
763
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
764
            [_pick selectRow:i  inComponent:0 animated:NO];
xwenliang's avatar
xwenliang committed
765
            _num=i;
xwenliang's avatar
xwenliang committed
766 767 768
            break;
        }
    }
xwenliang's avatar
xwenliang committed
769 770 771 772 773 774
    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
775
    }
xwenliang's avatar
xwenliang committed
776
    
xwenliang's avatar
xwenliang committed
777 778
    NSString *selectTwoStr;
    if (self.selectValueArry.count>1) {
xwenliang's avatar
xwenliang committed
779
        
xwenliang's avatar
xwenliang committed
780 781 782 783 784 785
        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
786
        
xwenliang's avatar
xwenliang committed
787 788
        if ([selectTwoStr isEqualToString:str]) {
            
xwenliang's avatar
xwenliang committed
789
            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
            [_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
809
                [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
                [_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
832
                            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
                            [_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
851
                            [_pick reloadAllComponents];
xwenliang's avatar
xwenliang committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
                            [_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
880
            //NSLog(@"%@---%@",a,b);
xwenliang's avatar
xwenliang committed
881 882 883 884 885 886 887 888 889 890 891 892 893
            [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
894 895
                
                [self.backArry addObject:[self.noCorreArry objectAtIndex:0]];
xwenliang's avatar
xwenliang committed
896 897 898
            }
            
        }else{
xwenliang's avatar
xwenliang committed
899
            //无关联的,直接给几个选项就行
xwenliang's avatar
xwenliang committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
            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
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
-(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];
        //在这里设置字体相关属性
        lbl.font = [UIFont systemFontOfSize:[_pickerFontSize integerValue]];
        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
950 951

@end