iOS UIPickerView的簡單封裝示例
前言
在iOS實(shí)際項(xiàng)目中,經(jīng)常會出現(xiàn)界面中多個地方需要使用UIPickerView,如果在每個需要用到的地方都創(chuàng)建一個UIPickerView不僅更耗性能,而且還會讓你的代碼變得更加雜亂、冗余,因此我在這里向大家介紹一下我對UIPickerView的一些簡單封裝。
所需屬性
/** pickerView*/ @property (nonatomic, strong) UIPickerView pickerView; /* pickerView背景*/ @property (nonatomic, strong) UIView pickerBackGroundView; /* 背景*/ @property (nonatomic, strong) UIView backGroundView; /* 確認(rèn)按鈕*/ @property (nonatomic, strong) UIButton sureButton; /* 取消按鈕*/ @property (nonatomic, strong) UIButton cancelButton; /* 單列pickerView*/ @property (nonatomic, strong) NSMutableArray slDataArray; /* 雙列pickerView*/ @property (nonatomic, strong) NSMutableArray *mulDataArray;
如果只需要一列的話,只需要傳入一個數(shù)據(jù)數(shù)組:slDataArray,如果需要兩行,則兩個數(shù)組都需要賦值。
實(shí)現(xiàn)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];
}
}
這里根據(jù)兩個數(shù)組來初始化pickerView的內(nèi)容,即判斷第二個數(shù)組(mulDataArray)是否有數(shù)據(jù),有數(shù)據(jù)的話代表加載兩列的pickerView,否則加載一列。
功能實(shí)現(xiàn)
-(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從下往上出現(xiàn)或者從上往下消失。
-(void)pickerViewReloadData{
[self.pickerView reloadAllComponents];
}
刷新pickerView數(shù)據(jù),加載另一個pickerView時,調(diào)用該方法刷新。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS sqlite對數(shù)據(jù)庫的各種操作(日常整理全)
在IOS中使用Sqlite來處理數(shù)據(jù)。如果你已經(jīng)了解了SQL,那你可以很容易的掌握SQLite數(shù)據(jù)庫的操作。本文給大家介紹iOS sqlite對數(shù)據(jù)庫的各種操作,需要的朋友參考下吧2016-03-03
iOS開發(fā)中使用CoreLocation框架處理地理編碼的方法
這篇文章主要介紹了iOS開發(fā)中使用CoreLocation框架處理地理編碼的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS實(shí)現(xiàn)逐幀動畫做loading視圖
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)逐幀動畫做loading視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
iOS使用核心動畫和粒子發(fā)射器實(shí)現(xiàn)點(diǎn)贊按鈕的方法
這篇文章主要給大家介紹了iOS如何使用核心動畫和粒子發(fā)射器實(shí)現(xiàn)點(diǎn)贊按鈕的方法,文中給出了詳細(xì)的示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒,有需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2016-12-12
iOS 中 使用UITextField格式化銀行卡號碼的解決方案
今天小編給大家分享ios中使用UITextField格式化銀行卡號碼的實(shí)現(xiàn)思路詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12

