UIPickerView的简单封装

2019-06-023590
李昌
iOS研发

UIPickerView的简单封装

在iOS实际项目中,经常会出现界面中多个地方需要使用UIPickerView,如果在每个需要用到的地方都创建一个UIPickerView不仅更耗性能,而且还会让你的代码变得更加杂乱、冗余,因此我在这里向大家介绍一下我对UIPickerView的一些简单封装。

所需属性

/* pickerView/</br> @property (nonatomic, strong) UIPickerView pickerView;</br> /** pickerView背景/</br> @property (nonatomic, strong) UIView pickerBackGroundView;</br> /** 背景/</br> @property (nonatomic, strong) UIView backGroundView;</br> /** 确认按钮/</br> @property (nonatomic, strong) UIButton sureButton;</br> /** 取消按钮/</br> @property (nonatomic, strong) UIButton cancelButton;</br> /** 单列pickerView/</br> @property (nonatomic, strong) NSMutableArray slDataArray;</br> /** 双列pickerView/</br> @property (nonatomic, strong) NSMutableArray *mulDataArray;</br>

如果只需要一列的话,只需要传入一个数据数组:slDataArray,如果需要两行,则两个数组都需要赋值。

实现UIPickerView代理方法

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{</br>      if (self.mulDataArray.count == 0) {</br>         return 1;</br>     }else {</br>         return 2;</br>         }</br> }</br> -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{</br>     if (component == 0) {</br>         return self.slDataArray.count;</br>         }else {</br>         return self.mulDataArray.count;</br>     }</br> }</br> -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:</br>(NSInteger)component{</br>     if (component == 0) {</br>         return self.slDataArray[row];</br>     }else {</br>     return self.mulDataArray[row];</br>     }</br> }</br>

这里根据两个数组来初始化pickerView的内容,即判断第二个数组(mulDataArray)是否有数据,有数据的话代表加载两列的pickerView,否则加载一列。

功能实现

-(void)pickerViewSelectRow:(NSInteger)row {</br>     self.selectRow = row;</br>     [self.pickerView selectRow:row inComponent:0 animated:NO];</br> }</br> -(void)pickerViewSelectRow:(NSInteger)row lastRow:(NSInteger)lastRow{</br>     [self.pickerView selectRow:row inComponent:0 animated:NO];</br>     [self.pickerView selectRow:lastRow inComponent:1 animated:NO];</br> }

第一个方法是只有一列的pickerView初始化是让其选中哪行,第二个则是两列的选择方法。

-(void)showOrHidePickerView:(BOOL)isShow{</br>     if (isShow) {</br>         if (self.isPickerShow == NO) {</br>             [self addSubview:self.backGroundView];</br>             [self addSubview:self.pickerBackGroundView];</br>             [UIView animateWithDuration:0.3 animations:^{</br>                  self.backGroundView.alpha = 0.5;</br>                  self.pickerBackGroundView.frame = CGRectMake(0, SCREEN_HEIGHT -220, SCREEN_WIDTH, 220);</br>             } completion:^(BOOL finished) {</br>           self.isPickerShow = YES;</br>             }];</br>         }</br>     }else {</br>         if (self.isPickerShow) {</br>             [UIView animateWithDuration:0.3 animations:^{</br>             self.backGroundView.alpha = 0.0;</br>             self.pickerBackGroundView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 220);</br>         } completion:^(BOOL finished) {</br>             [self.backGroundView removeFromSuperview];</br>             [self.pickerBackGroundView removeFromSuperview];</br>             self.isPickerShow = NO;</br>         }];</br>         }</br>     }</br> }</br>

这个方法是显示或者隐藏pickerView,通过动画的方式,背景慢慢变黑或者透明,pickerView从下往上出现或者从上往下消失。

-(void)pickerViewReloadData{</br>     [self.pickerView reloadAllComponents];</br> }</br>

刷新pickerView数据,加载另一个pickerView时,调用该方法刷新。

分享
点赞2
打赏
上一篇:iOS 中 Widget 的实现 | 实战系列
下一篇:iOS类本质探究(一)