您好,登錄后才能下訂單哦!
先說一下當個組件選取器,我們創建一個數組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];以上可以在視圖顯示一個選取器,但是內容空白,pickerView.showsSelectionIndicator=YES;是這只當前選取器所選中的內容:
選取器上顯示數據,必須依賴兩個協議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> { UIPickerView *pickerView; NSArray *pickerData; } @end3.然后在.m文件的ViewDidLoad中初始化界面
- (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]; }
#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/
UIPickerViewDelegate中的實例方法
-(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中的實例方法
按照官方文檔的說法,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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。