BzwPicker.m 33.4 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
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 32
        self.pickerToolBarFontSize=pickerToolBarFontSize;
        self.pickerFontSize=pickerFontSize;
        self.pickerFontColor=pickerFontColor;
33
        self.pickerRowHeight=pickerRowHeight;
xwenliang's avatar
xwenliang committed
34 35 36
        [self getStyle];
        [self getnumStyle];
        dispatch_async(dispatch_get_main_queue(), ^{
xwenliang's avatar
xwenliang committed
37
            [self makeuiWith:topbgColor With:bottombgColor With:leftbtnbgColor With:rightbtnbgColor With:centerbtnColor];
xwenliang's avatar
xwenliang committed
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
    [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
71
    [self.rightBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
72
    [self.rightBtn addTarget:self action:@selector(cfirmAction) forControlEvents:UIControlEventTouchUpInside];  
xwenliang's avatar
xwenliang committed
73
    
xwenliang's avatar
xwenliang committed
74 75 76
    UILabel *cenLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 5, SCREEN_WIDTH-180, 30)];
    
    cenLabel.textAlignment=NSTextAlignmentCenter;
xwenliang's avatar
xwenliang committed
77
    
xwenliang's avatar
xwenliang committed
78
    [cenLabel setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
79 80
    
    cenLabel.text=self.centStr;
xwenliang's avatar
xwenliang committed
81
    
xwenliang's avatar
xwenliang committed
82
    [cenLabel setTextColor:[self colorWith:centerbtnColor]];
xwenliang's avatar
xwenliang committed
83
    
xwenliang's avatar
xwenliang committed
84
    [view addSubview:cenLabel];
xwenliang's avatar
xwenliang committed
85

xwenliang's avatar
xwenliang committed
86
    self.pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height - 40)];
xwenliang's avatar
xwenliang committed
87 88 89 90 91 92 93 94 95 96 97
    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
98
        //这里是关联的
xwenliang's avatar
xwenliang committed
99 100 101 102
        if ([_numberCorrela isEqualToString:@"three"]) {
            return 3;
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
103
            
xwenliang's avatar
xwenliang committed
104 105 106 107 108 109 110 111 112 113 114
            return 2;
        }
        
    }
    //这里是不关联的
    if (_noArryElementBool) {
        
        return 1;
        
    }else{
        
xwenliang's avatar
xwenliang committed
115
        return self.noCorreArry.count;
xwenliang's avatar
xwenliang committed
116 117 118 119 120 121 122
    }
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_Correlation) {
        
xwenliang's avatar
xwenliang committed
123 124 125 126 127 128 129 130 131 132 133 134
        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
135 136
    }
    
xwenliang's avatar
xwenliang committed
137
    //NSLog(@"%@",[self.noCorreArry objectAtIndex:component]);
xwenliang's avatar
xwenliang committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    
    if (self.noCorreArry.count==1) {
        
        return [self.noCorreArry count];
        
    }else
    {
        
        if (_noArryElementBool) {
            
            return [self.noCorreArry count];
            
        }
        
xwenliang's avatar
xwenliang committed
152
        return  [[self.noCorreArry objectAtIndex:component] count];
xwenliang's avatar
xwenliang committed
153
    }
xwenliang's avatar
xwenliang committed
154
    
xwenliang's avatar
xwenliang committed
155 156 157 158 159 160 161 162 163
}

#pragma mark Picker Delegate Methods

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
xwenliang's avatar
xwenliang committed
311 312 313
    if (!row) {
        row=0;
    }
xwenliang's avatar
xwenliang committed
314
    [self.backArry removeAllObjects];
xwenliang's avatar
xwenliang committed
315 316
    [self.infoArry removeAllObjects];
    
xwenliang's avatar
xwenliang committed
317 318 319
    if (_Correlation) {
        //这里是关联的
        
xwenliang's avatar
xwenliang committed
320
        if ([_numberCorrela isEqualToString:@"three"]) {
xwenliang's avatar
xwenliang committed
321
            
xwenliang's avatar
xwenliang committed
322
            if (component == 0)
xwenliang's avatar
xwenliang committed
323
            {
xwenliang's avatar
xwenliang committed
324
                [self.cityArray removeAllObjects];
xwenliang's avatar
xwenliang committed
325
                
xwenliang's avatar
xwenliang committed
326 327
                NSInteger setline=[_pick selectedRowInComponent:0];
                
xwenliang's avatar
xwenliang committed
328 329 330 331 332 333 334 335 336
                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
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                
                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
364 365
            }
            
xwenliang's avatar
xwenliang committed
366
            if (component == 1)
xwenliang's avatar
xwenliang committed
367 368
            {
                
xwenliang's avatar
xwenliang committed
369
                NSInteger setline=[_pick selectedRowInComponent:0];
370
                
xwenliang's avatar
xwenliang committed
371 372 373 374 375
                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
376 377 378 379
                }
                
                [pickerView reloadAllComponents];
                [pickerView selectRow:0 inComponent:2 animated:YES];
xwenliang's avatar
xwenliang committed
380 381 382
                
            }
            
xwenliang's avatar
xwenliang committed
383
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
384
            
xwenliang's avatar
xwenliang committed
385
            if (component == 0)
xwenliang's avatar
xwenliang committed
386
            {
xwenliang's avatar
xwenliang committed
387 388 389 390 391 392 393 394 395 396 397 398
                [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
399
            }
xwenliang's avatar
xwenliang committed
400
            [pickerView reloadComponent:1];
xwenliang's avatar
xwenliang committed
401 402 403 404 405 406
            if (component==1) {
                [pickerView selectRow:row inComponent:1 animated:YES];
                
            }else{
                [pickerView selectRow:0 inComponent:1 animated:YES];
            }
xwenliang's avatar
xwenliang committed
407 408 409 410 411 412 413 414 415 416 417 418
        }
    }
    //返回选择的值就可以了
    
    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
419 420 421 422 423
            if (a&&b&&c) {
                [self.backArry addObject:a];
                [self.backArry addObject:b];
                [self.backArry addObject:c];
            }
xwenliang's avatar
xwenliang committed
424 425
            
        }else if ([_numberCorrela isEqualToString:@"two"]){
xwenliang's avatar
xwenliang committed
426
            
xwenliang's avatar
xwenliang committed
427 428
            NSString *a=[self.provinceArray objectAtIndex:[self.pick selectedRowInComponent:0]];
            NSString *b=[self.cityArray objectAtIndex:[self.pick selectedRowInComponent:1]];
xwenliang's avatar
xwenliang committed
429
            // NSLog(@"%@---%@",a,b);
xwenliang's avatar
xwenliang committed
430 431 432 433
            if (a&&b) {
                [self.backArry addObject:a];
                [self.backArry addObject:b];
            }
xwenliang's avatar
xwenliang committed
434 435 436 437
        }
        
    }else
    {
xwenliang's avatar
xwenliang committed
438
        
xwenliang's avatar
xwenliang committed
439 440 441 442 443
        if (_noArryElementBool) {
            
            [self.backArry addObject:[self.noCorreArry objectAtIndex:row]];
            
            
xwenliang's avatar
xwenliang committed
444 445 446 447 448 449 450 451 452
        }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
453 454 455 456 457
        }
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setValue:self.backArry forKey:@"selectedValue"];
xwenliang's avatar
xwenliang committed
458
    
xwenliang's avatar
xwenliang committed
459
    [dic setValue:@"select" forKey:@"type"];
460 461 462 463 464 465
    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
466
        [dic setValue:value forKey:@"selectedIndex"];
467
    }
xwenliang's avatar
xwenliang committed
468
    
xwenliang's avatar
xwenliang committed
469
    if (self.backArry.count>0) {
470
        self.bolock(dic);
xwenliang's avatar
xwenliang committed
471
    }
xwenliang's avatar
xwenliang committed
472
}
473 474 475 476 477 478

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

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

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

963 964 965 966 967 968 969 970 971 972 973 974 975 976
- (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
977
@end