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

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

@implementation BzwPicker

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

xwenliang's avatar
xwenliang committed
17 18 19 20 21
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backArry=[[NSMutableArray alloc]init];
xwenliang's avatar
xwenliang committed
22 23
        self.provinceArray=[[NSMutableArray alloc]init];
        self.cityArray=[[NSMutableArray alloc]init];
xwenliang's avatar
xwenliang committed
24
        self.selectValueArry=selectValueArry;
25
        self.weightArry=weightArry;
xwenliang's avatar
xwenliang committed
26 27 28 29
        self.pickerDic=dic;
        self.leftStr=leftStr;
        self.rightStr=rightStr;
        self.centStr=centerStr;
xwenliang's avatar
xwenliang committed
30 31 32
        self.pickerToolBarFontSize=pickerToolBarFontSize;
        self.pickerFontSize=pickerFontSize;
        self.pickerFontColor=pickerFontColor;
xwenliang's avatar
xwenliang committed
33 34 35
        [self getStyle];
        [self getnumStyle];
        dispatch_async(dispatch_get_main_queue(), ^{
xwenliang's avatar
xwenliang committed
36
            [self makeuiWith:topbgColor With:bottombgColor With:leftbtnbgColor With:rightbtnbgColor With:centerbtnColor];
xwenliang's avatar
xwenliang committed
37 38 39 40 41 42 43 44 45 46 47 48 49
            [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
50
    self.leftBtn.frame = CGRectMake(10, 5, 90, 30);
xwenliang's avatar
xwenliang committed
51
    [self.leftBtn setTitle:self.leftStr forState:UIControlStateNormal];
xwenliang's avatar
xwenliang committed
52
    [self.leftBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
53 54 55 56 57 58 59 60 61 62
    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
63
    self.rightBtn.frame = CGRectMake(view.frame.size.width-100,5, 90, 30);
xwenliang's avatar
xwenliang committed
64 65 66 67 68 69
    [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
70
    [self.rightBtn setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
71
    [self.rightBtn addTarget:self action:@selector(cfirmAction) forControlEvents:UIControlEventTouchUpInside];  
xwenliang's avatar
xwenliang committed
72
    
xwenliang's avatar
xwenliang committed
73 74 75
    UILabel *cenLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 5, SCREEN_WIDTH-180, 30)];
    
    cenLabel.textAlignment=NSTextAlignmentCenter;
xwenliang's avatar
xwenliang committed
76
    
xwenliang's avatar
xwenliang committed
77
    [cenLabel setFont:[UIFont systemFontOfSize:[_pickerToolBarFontSize integerValue]]];
xwenliang's avatar
xwenliang committed
78 79
    
    cenLabel.text=self.centStr;
xwenliang's avatar
xwenliang committed
80
    
xwenliang's avatar
xwenliang committed
81
    [cenLabel setTextColor:[self colorWith:centerbtnColor]];
xwenliang's avatar
xwenliang committed
82
    
xwenliang's avatar
xwenliang committed
83
    [view addSubview:cenLabel];
xwenliang's avatar
xwenliang committed
84

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

#pragma mark Picker Delegate Methods

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

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

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

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