先說一下當(dāng)個組件選取器,我們創(chuàng)建一個數(shù)組NSAray來保存選取器中的內(nèi)容;選取器本身不會儲存任何數(shù)據(jù),,它通過調(diào)用數(shù)據(jù)源和委托方法來顯示數(shù)據(jù);但是對于大量數(shù)據(jù)的數(shù)據(jù)源,數(shù)組并不合適,我們可以做一個靜態(tài)列表如plist文件或者URL載入,和后面將講在文件中獲取數(shù)據(jù),還以多個選取器的之間的關(guān)聯(lián)如何實現(xiàn);先說下簡單的單個選取器:
我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、宣州ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的宣州網(wǎng)站制作公司先把效果圖貼出來
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];以上可以在視圖顯示一個選取器,但是內(nèi)容空白,pickerView.showsSelectionIndicator=YES;是這只當(dāng)前選取器所選中的內(nèi)容:
選取器上顯示數(shù)據(jù),必須依賴兩個協(xié)議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
#import3.然后在.m文件的ViewDidLoad中初始化界面@interface ViewController : UIViewController { 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]; }
#pragma mark - #pragma mark Picker Date Source Methods //返回顯示的列數(shù) -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } //返回當(dāng)前列顯示的行數(shù) -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [pickerData count]; } #pragma mark Picker Delegate Methods //返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動的那個顯示欄上 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [pickerData objectAtIndex:row]; }前兩個是數(shù)據(jù)源的代理方法,一個是返回列,有幾個選取器就返回幾,第二個是設(shè)置選取器有多少行,因為就這一個選取器,所以直接返回行數(shù),即數(shù)組元素個數(shù)多少;第三個代理方法是將數(shù)組元素添加到了選取器上面顯示;
說一下兩個協(xié)議實例方法,參考http://www.cnblogs.com/edsioon/
UIPickerViewDelegate中的實例方法
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
作用: 當(dāng)用戶選擇某個row時,picker view調(diào)用此函數(shù)
參數(shù): pickerView representing the picker view request the data
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
作用:由picker view調(diào)用,當(dāng)其在繪制row內(nèi)容,需要row的高度時
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
作用: 當(dāng)picker view需要給指定的component.row指定title時,調(diào)用此函數(shù)
-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
作用: 當(dāng)picker view需要給指定的component.row指定view時,調(diào)用此函數(shù).返回值為用作row內(nèi)容的view
參數(shù): view參數(shù), 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
作用:當(dāng)picker view 需要row的寬度時,調(diào)用此函數(shù)
UIPickerViewDataSource中的實例方法
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。