先說一下當個組件選取器,我們創建一個數組NSAray來保存選取器中的內容;選取器本身不會儲存任何數據,,它通過調用數據源和委託方法來顯示數據;但是對於大量數據的數據源,數組並不合適,我們可以做一個靜態列表如plist文件或者URL載入,和後面將講在文件中獲取數據,還以多個選取器的之間的關聯如何實現;先說下簡單的單個選取器:
先把效果圖貼出來
1.新建工程名爲PickerViewDemo , File->New->Project ->single View Application -> next
2.在視圖上添加選取器
- pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
- // 指定Delegate
- pickerView.delegate=self;
- // 顯示選中框
- pickerView.showsSelectionIndicator=YES;
- [self.view addSubview:pickerView];
選取器上顯示數據,必須依賴兩個協議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
- {
- UIPickerView *pickerView;
- NSArray *pickerData;
- }
- @end
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
- // 指定Delegate
- pickerView.delegate=self;
- // 顯示選中框
- pickerView.showsSelectionIndicator=YES;
- [self.view addSubview:pickerView];
- NSArray *dataArray = [[NSArray alloc]initWithObjects:@"許嵩",@"周杰倫",@"梁靜茹",@"許飛",@"鳳凰傳奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永傳", nil];
- pickerData=dataArray;
- // 添加按鈕
- CGRect frame = CGRectMake(120, 250, 80, 40);
- UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- selectButton.frame=frame;
- [selectButton setTitle:@"SELECT" forState:UIControlStateNormal];
- [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:selectButton];
- }
4.實現UIPickerView的代理方法,將數據顯示在選取器上所需要幾個方法
- #pragma mark -
- #pragma mark Picker Date Source Methods
- //返回顯示的列數
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 1;
- }
- //返回當前列顯示的行數
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return [pickerData count];
- }
- #pragma mark Picker Delegate Methods
- //返回當前行的內容,此處是將數組中數值添加到滾動的那個顯示欄上
- -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- return [pickerData objectAtIndex:row];
- }
說一下兩個協議實例方法,參考http://www.cnblogs.com/edsioon/
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
作用: 當用戶選擇某個row時,picker view調用此函數
參數: pickerView representing the picker view request the data
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
作用:由picker view調用,當其在繪製row內容,需要row的高度時
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
作用: 當picker view需要給指定的component.row指定title時,調用此函數
-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
作用: 當picker view需要給指定的component.row指定view時,調用此函數.返回值爲用作row內容的view
參數: view參數, a view object that was previously used for this rows, but is now hidden and cached by the picker view
- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
作用:當picker view 需要row的寬度時,調用此函數
按照官方文檔的說法,UIPickerViewDataSource這個協議僅有的功能就是提供picker view中component的個數和各個component中的row的個數,雖然名爲datasource,但是它工作於MVC的C中
本協議僅有兩個實例方法,均需要實現
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
作用:返回pickerView應該有幾個component
-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
作用:返回指定component應該有幾個row
5.關於按鈕響應事件,關於按鈕的形成和添加響應事件不再提,前面都有,
- -(void) buttonPressed:(id)sender
- {
- NSInteger row =[pickerView selectedRowInComponent:0];
- NSString *selected = [pickerData objectAtIndex:row];
- NSString *message = [[NSString alloc] initWithFormat:@"你選擇的是:%@",selected];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
- message:message
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles: nil];
- [alert show];
- }
[pickerViewselectedRowInComponent:0];方法返回當前被選中的索引序號,這是UIPickerView的實例方法,在官方文檔中
UIPickerView還有其他實例方法
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
參數爲component的序號(從左到右,以0起始),返回指定的component中row的個數
-(void) reloadAllComponents
調用此方法使得PickerView向delegate: Query for new data for all components
-(void) reloadComponent: (NSInteger) component
參數爲需更新的component的序號,調用此方法使得PickerView向其delegate: Query for new data
-(CGSize) rowSizeForComponent: (NSInteger) component
參數爲component的序號,返回值爲the size of rows in the given components, picker view 通過調用委託方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:獲得返回值
-(NSInteger) selectedRowInComponent: (NSInteger) component
參數爲component的序號,返回被選中row的序號,若無row被選中,則返回-1
-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
作用:在代碼指定要選擇的某component的某row
參數:row序號,component序號,BOOL值(若爲YES,轉動spin到你選擇的新值,若爲NO,直接顯示你選擇的值)
-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
參數:row序號,component序號,返回由委託方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委託對象並沒有實現這個方法,或者說這個view並不是可見的,則返回nil
附上源代碼: http://download.csdn.net/detail/duxinfeng2010/4410909