标签:
前几天用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:0] allKeys];
}
if ([_subPickerArraycount]>0) {
self.thirdPickerArray=[[self.selectArray objectAtIndex:0] objectForKey:[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:0] allKeys];
}else{
self.subPickerArray=nil;
}
if ([self.subPickerArraycount]>0) {
self.thirdPickerArray=[[self.selectArray objectAtIndex:0] objectForKey:[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:0] objectForKey:[self.subPickerArray objectAtIndex:row]];
}else{
self.thirdPickerArray=nil;
}
[pickerView selectRow:0 inComponent:2 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
标签:
原文地址:http://www.cnblogs.com/zhun/p/5515580.html