iOS中如何封装UIPickerView
这篇文章主要介绍iOS中如何封装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时,调用该方法刷新。
以上是“iOS中如何封装UIPickerView”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享名称:iOS中如何封装UIPickerView
当前路径:http://pwwzsj.com/article/ghoeji.html