标签:io color ar os for sp 数据 on cti
#import "ViewController.h" @implementation ViewController NSArray *category; //第一列存储类别 NSArray *name; //第二列存储该列别下对应的名称 NSDictionary *data; NSString *defaultSel; //默认选择的项目 @synthesize pickerView; - (void)viewDidLoad { [super viewDidLoad]; //初始化数组 //category = [NSArray arrayWithObjects:@"人",@"动物",@"水果",@"蔬菜",nil]; name = [NSArray arrayWithObjects:@"狗",@"猫",@"乌龟",@"小鸡",nil]; data = [NSDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithObjects:@"男",@"女",@"老",@"幼",nil],@"人", [NSArray arrayWithObjects:@"狗",@"猫",@"乌龟",@"小鸡",nil],@"动物", [NSArray arrayWithObjects:@"橡胶",@"柿",@"栗子",@"苹果",nil],@"水果", [NSArray arrayWithObjects:@"白菜",@"萝卜",@"丝瓜",@"茄子",nil],@"蔬菜", nil]; //对key进行排序 category = [[data allKeys] sortedArrayUsingSelector:@selector(compare:) ]; defaultSel = [category objectAtIndex:0]; //默认选中的项 //初始化pickView pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,300, 0,0)]; // pickerView.contentMode = UIViewContentModeCenter; //为pickerView 设定数据源和代理 self.pickerView.dataSource = self; self.pickerView.delegate = self; //pickerView.alpha = 0.2; [pickerView setContentMode:UIViewContentModeBottom]; //[pickerView setBackgroundColor:[UIColor redColor]]; //添加到视图中 [self.view addSubview:pickerView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //该方法的返回值决定,pickerView 视图中将有几列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; //返回1,表示有1列 } //返回值决定,指定component 中包含多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) { return category.count; } return [[data objectForKey:defaultSel] count]; } // 返回值,将作为指定列表和列表项的文本 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return [category objectAtIndex:row]; } return [[data objectForKey:defaultSel] objectAtIndex:row]; } //当选中列表项中指定列时候触发 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { defaultSel = [category objectAtIndex:row]; [self.pickerView reloadComponent:1]; //从新加载第二列的数据 } /* NSArray *tmp = component == 0?category:name; NSString *tip = component == 0?@"类别":@"名称"; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"信息" message:[NSString stringWithFormat:@"你选中的是:%@,名称是:%@",tip,[tmp objectAtIndex:row]] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; [alertView show]; */ } //指定pickView列的宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component ==0) { return 90; } return 200; } @end
标签:io color ar os for sp 数据 on cti
原文地址:http://my.oschina.net/u/582870/blog/341365