码迷,mamicode.com
首页 > 其他好文 > 详细

UIPickerView实现省 市 区三级联动

时间:2016-05-21 21:43:46      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:

前几天用UIPickerView实现了一下三级联动具体的如下图

技术分享

下面是实现方式

 

 

#import "ViewController.h"

#define FirstComponent 0

#define SubComponent 1

#define ThirdComponent 2

@interface ViewController ()

@property(nonatomic,retain)NSDictionary* dict;

@property(nonatomic,retain)NSArray* pickerArray;

@property(nonatomic,retain)NSArray* subPickerArray;

@property(nonatomic,retain)NSArray* thirdPickerArray;

@property(nonatomic,retain)NSArray* selectArray;

@end


@implementation ViewController

@synthesize dict=_dict;

@synthesize pickerArray=_pickerArray;

@synthesize subPickerArray=_subPickerArray;

@synthesize thirdPickerArray=_thirdPickerArray;

@synthesize selectArray=_selectArray;

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    NSString* plistPath=[[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];

    _dict=[[NSDictionary alloc] initWithContentsOfFile:plistPath];

    self.pickerArray=[_dict allKeys];

    self.selectArray=[_dict objectForKey:[[_dict allKeys] objectAtIndex:0]];

   if ([_selectArray count]>0) {

        self.subPickerArray= [[self.selectArray objectAtIndex:0allKeys];

    }

    if ([_subPickerArraycount]>0) {

        self.thirdPickerArray=[[self.selectArray objectAtIndex:0objectForKey:[self.subPickerArray objectAtIndex:0]];

    }


    

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark --

#pragma mark--UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

   return3;

}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

   if (component==FirstComponent) {

       return [self.pickerArray count];

    }

   if (component==SubComponent) {

       return [self.subPickerArray count];

    }

   if (component==ThirdComponent) {

        return [self.thirdPickerArray count];

    }

   return0;

}



#pragma mark--

#pragma mark--UIPickerViewDelegate

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

   if (component==FirstComponent) {

       return [self.pickerArray objectAtIndex:row];

    }

   if (component==SubComponent) {

       return [self.subPickerArray objectAtIndex:row];

    }

   if (component==ThirdComponent) {

       return [self.thirdPickerArray objectAtIndex:row];

    }

    return nil;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    NSLog(@"row is %d,Component is %d",row,component);

   if (component==0) {

        self.selectArray=[_dict objectForKey:[self.pickerArray objectAtIndex:row]];

       if ([self.selectArray count]>0) {

            self.subPickerArray= [[self.selectArray objectAtIndex:0allKeys];

        }else{

           self.subPickerArray=nil;

        }

       if ([self.subPickerArraycount]>0) {

            self.thirdPickerArray=[[self.selectArray objectAtIndex:0objectForKey:[self.subPickerArray objectAtIndex:0]]; 

        }else{

            self.thirdPickerArray=nil;

        }

        [pickerView selectedRowInComponent:1];

        [pickerView reloadComponent:1];

        [pickerView selectedRowInComponent:2];

        

        

    }

   if (component==1) {

        if ([_selectArraycount]>0&&[_subPickerArraycount]>0) {

            self.thirdPickerArray=[[self.selectArray objectAtIndex:0objectForKey:[self.subPickerArray objectAtIndex:row]];

        }else{

            self.thirdPickerArray=nil;

        }

        [pickerView selectRow:inComponent:animated:YES];

        

    }

    

    

    [pickerView reloadComponent:2];

    

}




- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

   if (component==FirstComponent) {

       return90.0;

    }

   if (component==SubComponent) {

       return120.0;

    }

   if (component==ThirdComponent) {

       return100.0;

    }

   return0;

}

-(void)dealloc

{

   self.dict=nil;

   self.pickerArray=nil;

   self.subPickerArray=nil;

   self.thirdPickerArray=nil;

    self.selectArray=nil;

    [superdealloc];

}

@end

UIPickerView实现省 市 区三级联动

标签:

原文地址:http://www.cnblogs.com/zhun/p/5515580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!