UIPickerView的簡單封裝

UIPickerView的簡單封裝

在iOS實際項目中,常常會出現界面中多個地方須要使用UIPickerView,若是在每一個須要用到的地方都建立一個UIPickerView不只更耗性能,並且還會讓你的代碼變得更加雜亂、冗餘,所以我在這裏向你們介紹一下我對UIPickerView的一些簡單封裝。數組

所需屬性

/** pickerView*/
@property (nonatomic, strong) UIPickerView pickerView;
/
* pickerView背景*/
@property (nonatomic, strong) UIView pickerBackGroundView;
/
* 背景*/
@property (nonatomic, strong) UIView backGroundView;
/
* 確認按鈕*/
@property (nonatomic, strong) UIButton sureButton;
/
* 取消按鈕*/
@property (nonatomic, strong) UIButton cancelButton;
/
* 單列pickerView*/
@property (nonatomic, strong) NSMutableArray slDataArray;
/
* 雙列pickerView*/
@property (nonatomic, strong) NSMutableArray *mulDataArray;
ide

若是隻須要一列的話,只須要傳入一個數據數組:slDataArray,若是須要兩行,則兩個數組都須要賦值。性能

實現UIPickerView代理方法

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

這裏根據兩個數組來初始化pickerView的內容,即判斷第二個數組(mulDataArray)是否有數據,有數據的話表明加載兩列的pickerView,不然加載一列。atom

功能實現

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

第一個方法是隻有一列的pickerView初始化是讓其選中哪行,第二個則是兩列的選擇方法。component

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

這個方法是顯示或者隱藏pickerView,經過動畫的方式,背景慢慢變黑或者透明,pickerView從下往上出現或者從上往下消失。animation

-(void)pickerViewReloadData{
    [self.pickerView reloadAllComponents];
}
it

刷新pickerView數據,加載另外一個pickerView時,調用該方法刷新。

相關文章
相關標籤/搜索