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

如果只需要一列的话,只需要传入一个数据数组: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,220);
  } completion:^(BOOL finished) {
   [self.backGroundView removeFromSuperview];
   [self.pickerBackGroundView removeFromSuperview];
   self.isPickerShow = NO;
  }];
  }
 }
}

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

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...