国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS UIPickerView的簡單封裝示例

2020-07-26 02:19:48
字體:
來源:轉載
供稿:網友

前言

在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;

如果只需要一列的話,只需要傳入一個數據數組: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,否則加載一列。

功能實現

-(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初始化是讓其選中哪行,第二個則是兩列的選擇方法。

-(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;  }];  } }}

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

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

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 英吉沙县| 青冈县| 忻城县| 泗洪县| 唐山市| 社旗县| 寻乌县| 乐陵市| 灵寿县| 平阴县| 敦化市| 元谋县| 栾川县| 比如县| 鹤岗市| 漠河县| 磐安县| 台湾省| 吉木萨尔县| 岳西县| 沁水县| 武定县| 大荔县| 阿坝县| 富锦市| 广平县| 灌南县| 成武县| 钦州市| 宣汉县| 拉孜县| 元氏县| 洛隆县| 五华县| 嘉荫县| 阳新县| 仪陇县| 额尔古纳市| 东乌珠穆沁旗| 巴彦淖尔市| 内乡县|