UIPickerView的使用

来源:互联网 发布:sql select 创建新表 编辑:程序博客网 时间:2024/04/28 22:26

UIPickerView功能与UIDatePicker类似

初始化实例时,通常只需要设置原点坐标,不需要设置宽高(默认宽高为:frame = (0 0; 320 216)

区别在于:

1UIPickerView需要自定义实现数据类型(包括:数据源,列数等)


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource>  
  2.   
  3. @property (nonatomicstrongNSDictionary *sourceDict;  
  4. @property (nonatomicstrongNSString *rowTitleFirst;  
  5. @property (nonatomicstrongNSString *rowTitleSecond;  
  6. @property (nonatomicstrongNSString *rowTitleThird;  
  7.   
  8. @property (nonatomicstrongUITextField *textfield;  
  9.   
  10. @end  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UIPickerView *pickerView = [[UIPickerView alloc] init];  
  2. //    [self.view addSubview:pickerView];  
  3. pickerView.backgroundColor = [UIColor orangeColor];  
  4. NSLog(@"pickerView %@", pickerView);  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2. 设置代理 
  3. 1 包括代理对象,及数据源代理对象 
  4. 2 实现代理方法的对象 
  5. 3 添加协议 
  6. 4 实现代理方法 
  7.       
  8. 注意事项 
  9. 1、两种代理必须同时实现 
  10. 2、标题显示设置三选一,只设置其中一个方法即可 
  11. 2-1、- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
  12. 2-2、- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
  13. 2-3、- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view 
  14. */  
  15. pickerView.delegate = self;  
  16. pickerView.dataSource = self;  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 设置数据源  
  2. NSArray *meizhouArray = @[@"梅县"@"五华县"@"丰顺县"@"大埔县"@"蕉岭县"@"兴宁市"@"梅江区"];  
  3. NSArray *guangzhouArray = @[@"天河区"@"白云区"@"荔湾区"@"番禺区"@"海珠区"@"花都区"@"从化区"@"增城区"];  
  4. NSArray *shenzhenArray = @[@"龙华新区"@"大鹏新区"@"福田区"@"罗湖区"@"宝安区"@"龙岗区"@"南山区"];  
  5. NSDictionary *guangdongCityDict = [NSDictionary dictionaryWithObjectsAndKeys:shenzhenArray, @"深圳市", guangzhouArray, @"广州市", meizhouArray, @"梅州市", nil nil];  
  6. NSDictionary *guangxiCityDict = [NSDictionary dictionaryWithObjectsAndKeys:@[], @"柳州市", @[], @"桂林市", nil nil];  
  7. self.sourceDict = [NSDictionary dictionaryWithObjectsAndKeys:guangdongCityDict, @"广东省", guangxiCityDict, @"广西省", nil nil];  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 设置默认值  
  2. self.rowTitleFirst = @"广东省";  
  3. self.rowTitleSecond = @"深圳市";  
  4. [pickerView selectRow:0 inComponent:0 animated:NO];  
  5. [pickerView selectRow:0 inComponent:1 animated:NO];  
  6. [pickerView selectRow:3 inComponent:2 animated:NO];  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 刷新数据  
  2. [pickerView reloadAllComponents];  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 通过textfield来使用  
  2. self.textfield = [[UITextField alloc] initWithFrame:CGRectMake(10.050.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 40.0)];  
  3. [self.view addSubview:self.textfield];  
  4. self.textfield.backgroundColor = [UIColor yellowColor];  
  5. self.textfield.textColor = [UIColor redColor];  
  6. self.textfield.placeholder = @"请选择地址(省市区)";  
  7. // 添加键盘上方的子视图  
  8. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
  9. button.frame = CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 40.0);  
  10. button.backgroundColor = [UIColor greenColor];  
  11. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  12. [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];  
  13. [button setTitle:@"隐藏键盘" forState:UIControlStateNormal];  
  14. [button addTarget:self action:@selector(hiddenKeyboard:) forControlEvents:UIControlEventTouchUpInside];  
  15. self.textfield.inputAccessoryView = button;  
  16. //输入源改成地址选择视图  
  17. self.textfield.inputView = pickerView;  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // UIPickerViewDelegate  
  2.   
  3. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component  
  4. {  
  5.     // 设置列的宽度  
  6.     return 100.0;  
  7. }  
  8.   
  9. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component  
  10. {  
  11.     // 设置列中的每行的高度  
  12.     return 40.0;  
  13. }  
  14.   
  15. - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
  16. {  
  17.     // 设置列中的每行的显示标题NSString  
  18.       
  19.     // 设置每列每行的标题  
  20.     NSArray *keyArray = self.sourceDict.allKeys;  
  21.     if (0 == component)  
  22.     {  
  23.         NSString *title = keyArray[row];  
  24.         return title;  
  25.     }  
  26.     else if (1 ==component)  
  27.     {  
  28.         NSDictionary *cityDict = self.sourceDict[self.rowTitleFirst];  
  29.         NSArray *cityArray = cityDict.allKeys;  
  30.         NSString *title = cityArray[row];  
  31.         return title;  
  32.     }  
  33.     else if (2 == component)  
  34.     {  
  35.         NSDictionary *areaDict = self.sourceDict[self.rowTitleFirst];  
  36.         NSArray *areaArray = areaDict[self.rowTitleSecond];  
  37.         NSString *title = areaArray[row];  
  38.         return title;  
  39.     }  
  40.       
  41.     return nil;  
  42. }  
  43.   
  44. //- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component  
  45. //{  
  46. //    // 设置列中的每行的显示标题NSAttributedString  
  47. //    return nil;  
  48. //}  
  49.   
  50. //- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view  
  51. //{  
  52. //    // 设置列中的每行的自定义视图  
  53. //    return nil;  
  54. //}  
  55.   
  56. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  
  57. {  
  58.     // 获取列中选中的某一行  
  59.       
  60.     NSLog(@"component = %ld, row = %ld", component, row);  
  61.       
  62.     NSArray *keyArray = self.sourceDict.allKeys;  
  63.     if (0 == component)  
  64.     {  
  65.         NSString *keyFirst = keyArray[row];  
  66.         // 设置第一列的值,即key1  
  67.         self.rowTitleFirst = keyFirst;  
  68.         // 当第一列改变时,第二列的值应该跟着改变,即key2,设置为默认第二列的第一个值  
  69.         NSDictionary *cityDict = self.sourceDict[self.rowTitleFirst];  
  70.         NSArray *cityArray = cityDict.allKeys;  
  71.         if (cityArray && 0 < cityArray.count)  
  72.         {  
  73.             NSString *keySecond = cityArray[0];  
  74.             self.rowTitleSecond = keySecond;  
  75.         }  
  76.         // 设置第三个标题,当第二列改变时,第三wfhr值路着改变  
  77.         NSArray *areaArray = cityDict[self.rowTitleSecond];  
  78.         if (areaArray && 0 < areaArray.count)  
  79.         {  
  80.             NSString *keyThird = areaArray[0];  
  81.             self.rowTitleThird = keyThird;  
  82.         }  
  83.         else  
  84.         {  
  85.             // 没有第三列时,将第三列的默认标题设置为空  
  86.             self.rowTitleThird = nil;  
  87.         }  
  88.           
  89.         // 重新刷新第2列的数据,且设置显示为第一行  
  90.         [pickerView reloadComponent:1];  
  91.         [pickerView selectRow:0 inComponent:1 animated:YES];  
  92.           
  93.         // 重新刷新第3列的数据,且设置显示为第一行  
  94.         [pickerView reloadComponent:2];  
  95.         [pickerView selectRow:0 inComponent:2 animated:YES];  
  96.     }  
  97.     else if (1 ==component)  
  98.     {  
  99.         // 设置第二个标题  
  100.         NSDictionary *cityDict = self.sourceDict[self.rowTitleFirst];  
  101.         NSArray *cityArray = cityDict.allKeys;  
  102.         NSString *keySecond = cityArray[row];  
  103.         self.rowTitleSecond = keySecond;  
  104.           
  105.         // 设置第三个标题,默认第一行  
  106.         NSArray *areaArray = cityDict[self.rowTitleSecond];  
  107.         if (areaArray && 0 < areaArray.count)  
  108.         {  
  109.             NSString *keyThird = areaArray[0];  
  110.             self.rowTitleThird = keyThird;  
  111.         }  
  112.           
  113.           
  114.         // 重新刷新第3列的数据,且设置显示为第一行  
  115.         [pickerView reloadComponent:2];  
  116.         [pickerView selectRow:0 inComponent:2 animated:YES];  
  117.     }  
  118.     else if (2 == component)  
  119.     {  
  120.         // 设置第三个标题  
  121.         NSDictionary *cityDict = self.sourceDict[self.rowTitleFirst];  
  122.         NSArray *areaArray = cityDict[self.rowTitleSecond];  
  123.         if (areaArray && 0 < areaArray.count)  
  124.         {  
  125.             NSString *keyThird = areaArray[row];  
  126.             self.rowTitleThird = keyThird;  
  127.         }  
  128.     }  
  129.       
  130.     NSString *text = [NSString stringWithFormat:@"%@%@%@"self.rowTitleFirstself.rowTitleSecondself.rowTitleThird];  
  131.     NSLog(@"选择结果:%@", text);  
  132.       
  133.     self.textfield.text = text;  
  134. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // UIPickerViewDataSource  
  2.   
  3. // returns the number of 'columns' to display.  
  4. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView  
  5. {  
  6.     // 设置列数  
  7.       
  8.     // 设置三列:省、市、区  
  9.     return 3;  
  10. }  
  11.   
  12. // returns the # of rows in each component..  
  13. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component  
  14. {  
  15.     // 设置每列的行数  
  16.       
  17.     // 设置每列的实际行数  
  18.     NSArray *keyArray = self.sourceDict.allKeys;  
  19.     if (0 == component)  
  20.     {  
  21.         return keyArray.count;  
  22.     }  
  23.     else if (1 ==component)  
  24.     {  
  25.         NSDictionary *cityDict = self.sourceDict[self.rowTitleFirst];  
  26.         NSArray *cityArray = cityDict.allKeys;  
  27.         return cityArray.count;  
  28.     }  
  29.     else if (2 == component)  
  30.     {  
  31.         NSDictionary *areaDict = self.sourceDict[self.rowTitleFirst];  
  32.         NSArray *areaArray = areaDict[self.rowTitleSecond];  
  33.         return areaArray.count;  
  34.     }  
  35.       
  36.     return 0;  
  37. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 隐藏键盘  
  2. - (void)hiddenKeyboard:(UIButton *)button  
  3. {  
  4.     [self.view endEditing:YES];  
  5. }  





0 0