接上篇iOS學習之UIPickerView控制項的簡單使用
接著上篇的代碼 http://download.csdn.net/detail/totogo2010/4391870 ,我們要實現的效果如下:
當選擇左邊的一級選項時,左邊展示一級選項裡含有的二級選項,選擇後顯示在TextField裡。
如何?呢?建立一個和左邊的列表key對應的數組,當選擇這個key時,重新整理左邊UIPickerView部分的內容顯示對應數組的資料,選擇時,找到
兩個UIPickerView組件rowIndex,找出資料,放到TextField裡。
1、開啟上篇PickerViewDemo項目,在ViewController.h添加兩個成員變數:NSArray *subPickerArray; NSDictionary *dicPicker;
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIPickerViewDelegate, UITextFieldDelegate,UIPickerViewDataSource>{ NSArray *pickerArray; NSArray *subPickerArray; NSDictionary *dicPicker;}- (IBAction)selectButton:(id)sender;@property (strong, nonatomic) IBOutlet UIToolbar *doneToolbar;@property (strong, nonatomic) IBOutlet UIPickerView *selectPicker;@property (strong, nonatomic) IBOutlet UITextField *textField;@end
2、初始化
- (void)viewDidLoad{ [super viewDidLoad]; pickerArray = [NSArray arrayWithObjects:@"動物",@"植物",@"石頭", nil]; dicPicker = [NSDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithObjects:@"魚",@"鳥",@"蟲子", nil], @"動物", [NSArray arrayWithObjects:@"花",@"草",@"葵花", nil], @"植物", [NSArray arrayWithObjects:@"瘋狂的石頭",@"花崗岩",@"鵝卵石", nil], @"石頭",nil]; subPickerArray = [dicPicker objectForKey:@"動物"]; textField.inputView = selectPicker; textField.inputAccessoryView = doneToolbar; textField.delegate = self; selectPicker.delegate = self; selectPicker.dataSource = self; selectPicker.frame = CGRectMake(0, 480, 320, 216);}
給 NSDictionary *dicPicker;賦值,對應的三個關鍵字添加了對應的數組。
3、Component返回兩個,這樣就有兩個齒輪了。
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;}
4、使用宏
在#import "ViewController.h"下面定義兩個宏,代表UIPickerView齒輪的左邊的部分和右邊的部分。左邊的部分是0,右邊的是1.
#import "ViewController.h"
#define kFirstComponent 0
#define kSubComponent 1
5、判斷是那個齒輪,返回相應的資料的Count。
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if(component == kFirstComponent){ return [pickerArray count]; }else { return [subPickerArray count]; }}
6、根據component返回相應的String資料
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == kFirstComponent){ return [pickerArray objectAtIndex:row]; }else { return [subPickerArray objectAtIndex:row]; }}
7、拖動左邊的齒輪時,右邊的資料相應的Reload更新。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == kFirstComponent) { subPickerArray = [dicPicker objectForKey:[pickerArray objectAtIndex:row]]; [pickerView selectRow:0 inComponent:kSubComponent animated:YES]; [pickerView reloadComponent:kSubComponent]; }}
8、相應選擇的資料,並顯示在TextField上。
-(void)textFieldDidEndEditing:(UITextField *)textField{ NSInteger firstViewRow = [selectPicker selectedRowInComponent:kFirstComponent]; NSInteger subViewRow = [selectPicker selectedRowInComponent:kSubComponent]; NSString * firstString = [pickerArray objectAtIndex:firstViewRow]; NSString * subString = [[dicPicker objectForKey:[pickerArray objectAtIndex:firstViewRow]] objectAtIndex:subViewRow] ; NSString *textString = [[NSString alloc ] initWithFormat:@"您選擇了:%@%@%@", firstString, @" 裡的 ", subString]; self.textField.text = textString;}- (IBAction)selectButton:(id)sender { [textField endEditing:YES];}
大功告成,運行,點擊TextField,彈出:
最終代碼:http://download.csdn.net/detail/totogo2010/4393004
著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝