IOS開發之UIPickerView


 

      前言: UIPickerView 是一個選擇器控件, 它可以生成單列的選擇器,也可生成多列的選擇器,而且開發者完全可以自定義選擇項的外觀,因此用法非常靈活。UIPickerView 直接繼承了 UIView ,沒有繼承 UIControl ,因此,它不能像 UIControl 那樣綁定事件處理方法, UIPickerView 的事件處理由其委托對象完成。

 

UIPickerView 控件常用的屬性和方法如下:

     numberOfComponents: 獲取UIPickerView指定列中包含的列表項的數量。該屬性是一個只讀屬性。 

    showsSelectionIndicator: 該屬性控制是否顯示UIPickerView中的選中標記(以高亮背景作為選中標記)。 

    numberOfRowsInComponent: 獲取UIPickerView包含的列數量。 

    rowSizeForComponent: 獲取UIPickerView包含的指定列中列表項的大小。該方法返回一個CGSize對象。 

    selectRow:inComponent:animated:: 該方法設置選中該UIPickerView中指定列的特定列表項。最后一個參數控制是否使用動畫。  

    selectedRowInComponent:: 該方法返回該UIPickerView指定列中被選中的列表項。 

    viewForRow:forComponent:: 該方法返回該UIPickerView指定列的列表項所使用的UIView控件。 

    UIDatePicker控件只是負責該控件的通用行為,而該控件包含多少列,各列包含多少個列表項則由UIPickerViewDataSource對象負責。開發者必須為UIPickerView設置UIPickerViewDataSource對象,並實現如下兩個方法。 

    numberOfComponentsInPickerView:: 該UIPickerView將通過該方法來判斷應該包含多少列。 

    pickerView:numberOfRowsInComponent:: 該UIPickerView將通過該方法判斷指定列應該包含多少個列表項。   

    如果程序需要控制UIPickerView中各列的寬度,以及各列中列表項的大小和外觀,或程序需要為UIPickerView的選中事件提供響應,都需要為UIPickerView設置UIPickerViewDelegate委托對象,並根據需要實現該委托對象中的如下方法。 

     pickerView:rowHeightForComponent:: 該方法返回的CGFloat值將作為該UIPickerView控件中指定列中列表項的高度。 

     pickerView:widthForComponent:: 該方法返回的CGFloat值將作為該UIPickerView控件中指定列的寬度。 

    pickerView:titleForRow:forComponent:: 該方法返回的NSString值將作為該UIPickerView控件中指定列的列表項的文本標題。 

    pickerView:viewForRow:forComponent:reusingView:: 該方法返回的UIView控件將直接作為該UIPickerView控件中指定列的指定列表項。 

    pickerView:didSelectRow:inComponent:: 當用戶單擊選中該UIPickerView控件的指定列的指定列表項時將會激發該方法 

    Interface Builder只支持為UIPickerView設置一個屬性——Shows Selection Indicator,該屬性用於控制是否顯示UIPickerView中的選中標記(以高亮背景作為選中標記)。

 

實例: 通過前面的知識普及, 我們知道了UIPickerView的使用方法, 下面通過一個小例子,來更全面的理解。

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>{

    //順便掛上兩個代理

    NSArray *array ;//定義一個全局數組作為PickView里顯示內容的  

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

//    數據源數組

 

    array = @[@"1",@"2",@"3",@"4",@"5"];

    

//    創建視圖

    UIPickerView *pickView = [[UIPickerView alloc]initWithFrame:self.view.frame];

    pickView.backgroundColor = [UIColor orangeColor];

    

//    掛上代理

    pickView.delegate = self;

    pickView.dataSource = self;

    

    pickView.showsSelectionIndicator = YES;

    

    [self.view addSubview:pickView];

    

    

    

}

//有幾列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

 

    return array.count;

}

//有幾行

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

 

    return 4;

}

//設置每一列的內容   返回值是一個字符串

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return array[row];

 

}

//點擊選中的方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    

    NSString *a = [NSString stringWithFormat:@"%ld %ld",(long)row,(long)component];

//    選中時  彈出提示框

    UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"馬薩卡" message:a preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"點擊完成");

    }];

    [aler addAction:action];

    [self presentViewController:aler animated:YES completion:nil];

    

    

}

 

執行效果如下:

 

-----------------------------------------

#pragma mark - 該方法返回的NSString將作為UIPickerView中指定列和列表項的標題文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (0 == component) { return _province[row]; } if(1 == component){ NSInteger rowProvince = [pickerView selectedRowInComponent:0]; NSString *provinceName = _province[rowProvince]; NSArray *citys = _city[provinceName]; return citys[row]; }else{ NSInteger rowProvince = [pickerView selectedRowInComponent:0]; NSString *provinceName = _province[rowProvince]; NSArray *citys = _city[provinceName]; NSInteger rowCity = [pickerView selectedRowInComponent:1]; NSString *cityName = citys[rowCity]; NSArray *country = _country[cityName]; return country[row]; } }

#pragma mark - 當用戶選中UIPickerViewDataSource中指定列和列表項時激發該方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {  if(0 == component){   [pickerView reloadComponent:1];   [pickerView reloadComponent:2];  }  if(1 == component)   [pickerView reloadComponent:2];   NSInteger rowOne = [pickerView selectedRowInComponent:0];   NSInteger rowTow = [pickerView selectedRowInComponent:1];   NSInteger rowThree = [pickerView selectedRowInComponent:2];   NSString *provinceName = _province[rowOne];   NSArray *citys = _city[provinceName];   NSString *cityName = citys[rowTow];   NSArray *countrys = _country[cityName];   NSLog(@"%@~%@~%@", _province[rowOne], citys[rowTow],countrys[rowThree]); }
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM