iOS – UIPickerView的使用

栏目: IOS · 发布时间: 6年前

内容简介:在日常的开发中,日期或者城市的等选择器功能的几乎是每个APP必有的。所以了解这些选择器的实现原理就显得格外的重要了。而我们日常实现一个滚动式的选择器大多是继承系统自带的UIPickerView-这个控件。通过查看UIPickerView的系统文件可以发现UIPickerView主要是引用了Foundation、CoreGraphics和UIKit框架,同时UIPickerView继承自UIView所以很自然的它拥有UIView的所拥有的特性。同时可以发现,UIPickerView有类似于UITableVie

在日常的开发中,日期或者城市的等选择器功能的几乎是每个APP必有的。所以了解这些选择器的实现原理就显得格外的重要了。而我们日常实现一个滚动式的选择器大多是继承系统自带的UIPickerView-这个控件。

iOS – UIPickerView的使用

通过查看UIPickerView的系统文件可以发现UIPickerView主要是引用了Foundation、CoreGraphics和UIKit框架,同时UIPickerView继承自UIView所以很自然的它拥有UIView的所拥有的特性。

同时可以发现,UIPickerView有类似于UITableView的数据源和代理方法,其实这也说明了UIPickerView的代码书写和UITableView还是很相像的,UIPickerView也有组和行的概念,但是UIPickerView每一组代表一个竖列。也就是“一个滚轮代表一组”。

通过@property(nonatomic,readonly) NSInteger numberOfComponents;可以获取选择器的分组数。

然后就是实现UIPickerView的代理方法了。

//获取指定分组component中的行数
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
//获取单元格的Size
- (CGSize)rowSizeForComponent:(NSInteger)component;
//刷新所有的组
- (void)reloadAllComponents;
//刷新指定分组
- (void)reloadComponent:(NSInteger)component;
//选中指定分组中的一行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
//获得指定分组中选中的行
- (NSInteger)selectedRowInComponent:(NSInteger)component;

UIPickerView有两个必须要声明的数据源代理方法

@protocol UIPickerViewDataSource<NSObject>@required
//一共多少组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 每一组多少列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end

然后就是UIPickerViewDelegate部分(一些常用的)

@protocol UIPickerViewDelegate<NSObject>
//返回的是一个UIView对象,所以可选就很多了,比如实现“国旗”选择器,放国家的图片、或者字体的颜色等需要重新定义的可以用UILable 等等
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view ;
//返回一个字符串,用的比较多,比如时间和地点信息等
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component ;
//这个和上面的类似,只是返回一个富文本字符串- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; 

//返回设置的每一组的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;
//返回设置的每一组中每一行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
//选中了哪一行,也就是滚轮滚到了那里。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;

二、UIPickerView简单的实现

iOS – UIPickerView的使用
//懒加载数组
-(NSArray *)foods{    
if (!_foods) {        
NSString *str = [[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil];
        _foods = [NSArray arrayWithContentsOfFile:str];
    }    
    return _foods ;
}

- (void)viewDidLoad {
    

[super viewDidLoad]

;          _pickview = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 70, self.view.bounds.size.width, 200)];     _pickview.backgroundColor = [UIColor whiteColor];     [_pickview reloadAllComponents];         self.pickview.delegate = self ;         self.pickview.dataSource = self ;

[self.view addSubview:_pickview]

; }#pragma mark  UIPickerViewDataSource //确定列 – (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{     return self.foods.count ; }/ /确定行 – (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{         NSArray *arr =  self.foods[component] ;         return arr.count ; } #pragma mark  UIPickerViewDelegate/ /显示每一列的内容 – (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {    if (component==0) {        if (row == 0) {             _str1 = @”” ;         }else{         _str1 = self.foods[component][row];         }     }    if (component ==1) {             if (row == 0) {             _str2 = @”” ;         }else{             _str2 = self.foods[component][row];         }     }    if (component ==2) {             if (row == 0) {             _str3 = @”” ;         }else{             _str3 = self.foods[component][row];         }     }     _lable.text = [NSString stringWithFormat: @”选择了%@-%@-%@”,_str1,_str2,_str3]; }

这样上面的例子就实现了。请大家不要吝啬,提出自己的建议,让我改正,共鸣。

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/25410.html

iOS – UIPickerView的使用

iOS – UIPickerView的使用 微信打赏

iOS – UIPickerView的使用 支付宝打赏

感谢您对作者Miya的打赏,我们会更加努力!    如果您想成为作者,请点我


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Haskell School of Music

The Haskell School of Music

Paul Hudak、Donya Quick / Cambridge University Press / 2018-10-4 / GBP 42.99

This book teaches functional programming through creative applications in music and sound synthesis. Readers will learn the Haskell programming language and explore numerous ways to create music and d......一起来看看 《The Haskell School of Music》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具