BzwPicker.m 33.5 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];
51 52
    self.leftBtn.frame = CGRectMake(0, 0, 90, 40);
    [self.leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10.0, 0, 0)];
xwenliang's avatar
xwenliang committed
53
    [self.leftBtn setTitle:self.leftStr forState:UIControlStateNormal];
xwenliang's avatar
xwenliang committed
54
    [self.leftBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
55 56 57 58 59 60 61 62 63 64
    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];
65 66
    self.rightBtn.frame = CGRectMake(view.frame.size.width-90,0, 90, 40);
    [self.rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10.0)];
xwenliang's avatar
xwenliang committed
67 68 69 70 71 72
    [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
73
    [self.rightBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
74
    [self.rightBtn addTarget:self action:@selector(cfirmAction) forControlEvents:UIControlEventTouchUpInside];  
xwenliang's avatar
xwenliang committed
75
    
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

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

#pragma mark Picker Delegate Methods

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

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

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

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

//按了取消按钮
-(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
600 601
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
        
xwenliang's avatar
xwenliang committed
602 603 604 605 606 607
        self.bolock(dic);
    }else{
        [self getNOselectinfo];
        
        [dic setValue:self.backArry forKey:@"selectedValue"];
        [dic setValue:@"cancel" forKey:@"type"];
xwenliang's avatar
xwenliang committed
608
        [dic setValue:[self getselectIndexArry] forKey:@"selectedIndex"];
xwenliang's avatar
xwenliang committed
609 610
        self.bolock(dic);
    }
xwenliang's avatar
xwenliang committed
611 612
    
    
xwenliang's avatar
xwenliang committed
613 614 615 616 617 618 619
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.2f animations:^{
            
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 250)];
            
        }];
    });
620 621

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

967 968 969 970 971 972 973 974 975 976 977 978 979 980
- (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
981
@end